This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Split a String

 
Michaela Fricker
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to split a Sting that is seperated by "..".
I tried:
String [] source = zeile.split("..");
But it did'n work, so I tried instead:
String [] source = zeile.split(",");
And it worked, but I need to split the text by "..".
Can anybody hlep me?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never used it myself, but unless I miss my guess it's because your regular expression is incorrectly formatted. Are you trying to split on a string containing two periods? If so, (since the period is a special character in regular expressions) you'll need to specify the regex as something along the lines of "\\.\\." (the double-backslash turns into a single in the string literal, which in turn escapes the period character).
hth,
bear
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afaik, "." is a special character in Regex, meaning "any character". If you mean a the character ".", you will need to escape it with a backslash. (See http://java.sun.com/j2se/1.4.1/docs/api/java/util/regex/Pattern.html#sum )
So, try split("\.\.") instead.
Did that help?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We normally never get to say this here, but just this once: ignore Ilja's post. Bear has it right - each . requires two \ in front of it. One is an escape for the Java compiler as it processes the String literal, and the other is an escape for the java.util.regex.Pattern object which is used by split() to parse the pattern string.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
We normally never get to say this here, but just this once: ignore Ilja's post. Bear has it right - each.

Gosh, he is! Nevertheless you might at least want to take a look at the link...
 
Michaela Fricker
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Baer, your post helped me quite well.
And Ilja: I made the same mistake as you...
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To split a delimited string , try using a StringTokenizer class.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevertheless you might at least want to take a look at the link...
Well OK, I suppose... :roll:
To split a delimited string , try using a StringTokenizer class.
Nope. StringTokenizer is capable of recognizing only single characters as delimiters, not longer strings (e.g. ".." in this case). Once you tell it a '.' is a delimiter, it will consider ".", "..", "...", etc all as equivalent. StringTokenizer just has too many headaches embedded in its API. Learning the syntax of regex patterns is much more useful, too, in the long run.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
StringTokenizer just has too many headaches embedded in its API. Learning the syntax of regex patterns is much more useful, too, in the long run.

When you realize that StringTokenizer was written to parse Java programs then it begins to make sense. It is designed to parse statements using the space as a separator. In a Java program the number of spaces is irrelevant so StringTokenizer treats one space and 20 spaces exactly the same.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic