File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Split String seperated by "."

 
Mike Himstead
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to escape it like this: "\\." (two backslashes because the Java string escape rules require one as well)
 
Anurag Mishra
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Himstead
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic