tags:

views:

52

answers:

4

Here's the code I'm using :

public class splitText {
public static void main(String[] args) {
    String x = "I lost my Phone. I shouldn't drive home alone";
    String[] result = x.split(".");
    for (String i : result) {
        System.out.println(i);
    }
}
}

Compiles perfectly, but nothing happens at runtime. What am I doing wrong?

+3  A: 

Try

String [] result = x.split("\\.");

Split takes in a pattern, not a character to split on. The "." is treated special in patterns.

Pace
+5  A: 

String.split uses a regex, so dot (.) means "anything". You need to escape the dot

public static void main(String[] args) {
    String x = "I lost my Phone. I shouldn't drive home alone";
    String[] result = x.split("\\.");
    for (String i : result) {
        System.out.println(i.trim());
    }
}

gives :

I lost my Phone
I shouldn't drive home alone
Jean-Philippe Caruana
+3  A: 

If you don't want to use regex, you can use the Splitter of guava lib

http://guava-libraries.googlecode.com/svn/trunk/javadoc/index.html

 String x = "I lost my Phone. I shouldn't drive home alone";
 Splitter.on('.').trimResults().split(x)

moreover, the result is an Iterable, not an array

Sylvain M
+1 for Guava =)
polygenelubricants
+6  A: 

String.split(String regex) takes a regular-expression pattern. It just so happens that . in regex is a metacharacter that matches (almost) any character, hence why split(".") doesn't work the way you expected.

You can escape the . by preceding it with a backslash. As a Java string literal, this is "\\.". The \ is doubled because \ itself is a Java escape character. "\\." is a String of length 2, containing a backslash and a period.

If you're given an arbitrary String that is to be matched literally (or if you just don't care to escape them yourself), you can use Pattern.quote. It'll make a pattern to literally match a given String.

See also


This is provided for educational purposes only:

    String text =
        "Wait a minute... what?!? Oh yeah! This is awesome!!";

    for (String part : text.split("(?<=[.?!]) ")) {
        System.out.println(part);
    }

This prints:

Wait a minute...
what?!?
Oh yeah!
This is awesome!!

References

Related questions

polygenelubricants
+1 for the example :)
Sylvain M
""\\." is a String of length 2, containing a backslash and a period." Umm.. I didn't get that. Can anyone re-explain it?
MoonStruckHorrors
@MoonStruckHorrors: A `String s = "\n";` contains one character, the newline character. `"\t"` contains a tab. `"\\"` contains a backslash. `"\""` contains a double quote. This is called an escape sequence. So since `"\\"` contains a backslash, `"\\."` contains a backslash and a period. You can print it out and check its `length()` to confirm. See also http://stackoverflow.com/questions/3224337/doubt-about-java-char/
polygenelubricants
Thanks. But doesn't that mean it'll split the string at `\.` and not `.` ?
MoonStruckHorrors
@MoonStruck: the regular expression pattern `\.` matches a literal period. The regular expression pattern `.` matches (almost) any character. So the pattern `a.b` matches strings like `"axb"`, `"a!b"`, etc. The pattern `a\.b` matches only `"a.b"`.
polygenelubricants
Excellent. Thanks again.
MoonStruckHorrors