This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Split a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Split a String" Watch "Split a String" New topic
Author

Split a String

Michaela Fricker
Greenhorn

Joined: Oct 28, 2002
Posts: 16
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

Joined: Jan 10, 2002
Posts: 60785
    
  65

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?


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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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
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
Thanks a lot Baer, your post helped me quite well.
And Ilja: I made the same mistake as you...
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
To split a delimited string , try using a StringTokenizer class.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
 
subject: Split a String
 
Similar Threads
Get the characters from String.
unix split command
String.split Vs StringTokenizer
SPLIT METHOD IN STRING CLASS
Command LIne Arguments