• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

string splitting

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

I have the following scenario:
ArrayList<String> list;
list contains string in the following format:
www.xyz.com
test.xyz.com
img1.test.xyz.com
a.abc.com
etc...

I want to split each string in the arraylist like->
www xyz.com
test xyz.com
img1.test xyz.com

How should I follow this up??

I am totally new to regex

thanks!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want to split each string in the arraylist like->
www xyz.com
test xyz.com
img1.test xyz.com

How should I follow this up??



Well, what is the exact criteria? It doesn't look like you want to split on every ".". And since it doesn't, how do you determine so?

Henry
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well it looks like he wants to split on the second . from the end.

You can use String.lastIndexOf for finding the first, then the second, then use String.substring for splittint.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Regex explanation: a dot (quoted as it is a metacharacter in regex) with a non-capturing zero-width positive lookahead to one or more non-dots, followed by a dot, followed again by one or more non-dots, followed by the end of the String.

Replace both + with * if the dots are not compulsorily separated by non-dot characters e.g. if
abcd.efg..
has to yield
abcd.efg
.
 
sanjay kumar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for the elaborate replies,
Yes Rob, you got it correct, I want to split from the second "." from the end

www.xyz.xom -> www & xyz.com
test.www.xyz.com0> test.www & xyz.com
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't feel like running the SSCCE I posted, huh?

Why do I bother...
 
sanjay kumar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Burke,

Sorry, for not replying upto your expectation...
Do you need a "thank" you explicitly...
I dont think because you are doing more than a "very good job"

I did try your code , but what i went ahead was this




I have another problem now

i am able to get the required in an arraylist, but how to count the occurence of each String ?
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's ok, even if you didn't use the regex solution it's more than welcome to know that you tried it.

To get any kind of a meaningful response, I think you'll need to explain just what you mean by

count the occurence of each String ?

 
sanjay kumar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the ArrayList<String> list;
For example : It contains xyz.com,xyz.com,abc.com,xyz.com,test.com..

Now I want a no of occurance of each string..
example
xyz.xom=3
abc.com=1
test.com=1
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We don't simply give out answers.

Putting them into a List is easy. The counting is probably easiest done with another sort of data structure. If you aren't familiar with the collections framework, look at the Java Tutorials.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and this is starting to look like a beginner's question.
 
sanjay kumar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do have my code & pieces in place sir!!!
Im NOT ASKING for your code here..,
I have my solution ready, but doesnt look too optimal, I cannot post it here ..So just looking for some suggestions from some experienced people..
Seems that hurt you ..
 
sanjay kumar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And yeah, use of the "Collections Framework" makes most of my bread-n-butter!!!
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can only help you improve your code if we see what you have already.
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sanjay kumar:
And yeah, use of the "Collections Framework" makes most of my bread-n-butter!!!



Take it easy, sport.
You can use a java.util.Map<Key, Value> to store your url's in. The Key would be your url, and the Value would be an Integer (the number of times the Key occurred).

Before you add an url to your map you first check if it's not already in there. If it is, increase the counter (the Value) by one and if it isn't, just add it in your map with the initial value of 1.


Map tutorial: http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic