my dog learned polymorphism*
The moose likes Beginning Java and the fly likes string splitting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string splitting" Watch "string splitting" New topic
Author

string splitting

sanjay kumar
Greenhorn

Joined: Aug 03, 2007
Posts: 21
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
Sheriff

Joined: Sep 28, 2004
Posts: 18500
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5


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
.


luck, db
There are no new questions, but there may be new answers.
sanjay kumar
Greenhorn

Joined: Aug 03, 2007
Posts: 21
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

Joined: May 03, 2008
Posts: 4523
    
    5

Didn't feel like running the SSCCE I posted, huh?

Why do I bother...
sanjay kumar
Greenhorn

Joined: Aug 03, 2007
Posts: 21
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

Joined: May 03, 2008
Posts: 4523
    
    5

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

Joined: Aug 03, 2007
Posts: 21
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

Joined: Oct 13, 2005
Posts: 37900
    
  22
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

Joined: Oct 13, 2005
Posts: 37900
    
  22
. . . and this is starting to look like a beginner's question.
sanjay kumar
Greenhorn

Joined: Aug 03, 2007
Posts: 21
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

Joined: Aug 03, 2007
Posts: 21
And yeah, use of the "Collections Framework" makes most of my bread-n-butter!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
We can only help you improve your code if we see what you have already.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string splitting
 
Similar Threads
List & ArrayList
Generics Problem
need help
GET does not include JSESSIONID
jsp pages not running without putting 'www' on url