14

I'm getting a string from the web looking like this:

Latest Episode@04x22^Killing Your Number^May/15/2009

Then I need to store 04x22, Killing Your Number and May/15/2009 in diffent variables, but it won't work.

String[] all = inputLine.split("@");
String[] need = all[1].split("^");
show.setNextNr(need[0]);
show.setNextTitle(need[1]);
show.setNextDate(need[2]);

Now it only stores NextNr, with the whole string

04x22^Killing Your Number^May/15/2009

What is wrong?

5 Answers 5

33

String.split(String regex)

The argument is a regualr expression, and ^ has a special meaning there; "anchor to beginning"

You need to do:

String[] need = all[1].split("\\^");

By escaping the ^ you're saying "I mean the character '^' "

Sign up to request clarification or add additional context in comments.

3 Comments

Splitting on a regular expression is the slowest of them all. Take a look at Guava's Splitter class.
Often (or let's say ... always), suggesting that someone include a giant dependency in their project for something that would shave a few nanoseconds which you don't actually care about and wouldn't matter one iota ... isn't exactly good advice. Also see: Premature optimization.
Guava is a well designed generic library that nicely complements the jre libs. It is not 'giant', nor only useful for this one problem. BTW, String.split() does compile the regular expression each time it is executed. Using it outside tests and initialization is highly discouraged.
23

If you have a separator but you don't know if it contains special characters you can use the following approach

String[] parts = Pattern.compile(separator, Pattern.LITERAL).split(text);

Comments

7

Using guava, you can do it elegantly AND fast:

private static final Splitter RECORD_SPLITTER = Splitter.on(CharMatcher.anyOf("@^")).trimResults().omitEmptyStrings();

...

Iterator<String> splitLine = Iterables.skip(RECORD_SPLITTER.split(inputLine), 1).iterator();

show.setNextNr(splitLine.next());
show.setNextTitle(splitLine.next());
show.setNextDate(splitLine.next());

Comments

1
public static String[] split(String string, char separator) {
    int count = 1;
    for (int index = 0; index < string.length(); index++)
        if (string.charAt(index) == separator)
            count++;
    String parts[] = new String[count];
    int partIndex = 0;
    int startIndex = 0;
    for (int index = 0; index < string.length(); index++)
        if (string.charAt(index) == separator) {
            parts[partIndex++] = string.substring(startIndex, index);
            startIndex = index + 1;
        }
    parts[partIndex++] = string.substring(startIndex);
    return parts;
}

Comments

1
String input = "Latest Episode@04x22^Killing Your Number^May/15/2009";

//split will work for both @ and ^
String splitArr[] = input.split("[@\\^]");

/*The output will be,
 [Latest Episode, 04x22, Killing Your Number, May/15/2009]
*/
System.out.println(Arrays.asList(splitArr));

2 Comments

Could you explain how this answers the question?
String can be split based upon multiple conditions. Here i used @ and ^ to split a string. It both will work as a delimiter and will split the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.