This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string splitting

 
sanjay kumar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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!
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
And yeah, use of the "Collections Framework" makes most of my bread-n-butter!!!
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can only help you improve your code if we see what you have already.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic