Split a String

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?

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

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

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.

"I'm not back." - Bill Harding, Twister

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112

posted Nov 05, 2002 23:57:00

0

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

Joined: Oct 28, 2002
Posts: 16

posted Nov 06, 2002 00:18:00

0

Thanks a lot Baer, your post helped me quite well. And Ilja: I made the same mistake as you...

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.

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.