aspose file tools*
The moose likes Java in General and the fly likes Split String seperated by Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Split String seperated by "."" Watch "Split String seperated by "."" New topic
Author

Split String seperated by "."

Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
Hi,

I have a String containing the classes package structur, like this.is.a.package

I'd like to split it, so I'd do something like:

String[] tokens = packageStructur.split(???)

Since "." is a special character when it comes to regular expressions, how can I split by "."? I can't find a soluation for this in the API.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
You need to escape it like this: "\\." (two backslashes because the Java string escape rules require one as well)


Ping & DNS - my free Android networking tools app
Anurag Mishra
Ranch Hand

Joined: Jun 16, 2003
Posts: 121
Hi,

One solution is there you can use the String class
method replace(CharSequence target, CharSequence replacement)

to replace all occurenec of . to | or something else.

And then the split will work and all the words seperated by . will
be stored in a String array.

thanks


SCJP 1.2
Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
Originally posted by Ulf Dittmer:
You need to escape it like this: "\\." (two backslashes because the Java string escape rules require one as well)


Some kind of double escape, I didn't think of that and give it a try. Thanks to both of you.
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
True, but then you would have to escape the pipe, because it's another special regex character. Also, you would have to be running JDK 1.5 or later, because that's when that form of the replace() method was added. (This is in reply to Anurag's post.)
[ May 03, 2006: Message edited by: Alan Moore ]
Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
Originally posted by Alan Moore:
True, but then you would have to escape the pipe, because it's another special regex character. Also, you would have to be running JDK 1.5 or later, because that's when that form of the replace() method was added. (This is in reply to Anurag's post.)

[ May 03, 2006: Message edited by: Alan Moore ]


replace is available in 1.4.2 (which is what I use), I already thought of that, but that sounds more like a workaround (which I'm fine with).
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
Originally posted by Mike Himstead:


replace is available in 1.4.2 (which is what I use), I already thought of that, but that sounds more like a workaround (which I'm fine with).

replace(char, char) has always been there, but replace(CharSequence, CharSequence) was added in 1.5.0. Either version would work in this case, but that's a moot point. If you want to split on a punctuation character, and you aren't sure if it has a special meaning, put backslashes in front of it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Split String seperated by "."