2

I have this string

String x = "2013-04-17T08:00:00.001,41.14806,-9.58972,-13.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-22.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-31.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-40.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-49.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-58.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-64.0,0.0,0.0,-20.0,4";

if i'm doing the split like this String vec2 [] = x.split(","); the output it will be this

2013-04-17T08:00:00.001
41.14806
-9.58972
-13.0
0.0
0.0
-20.0

and so on.

If I'm doing the split like this String vec2[] = x.split("|"); the output is this:

2
0
1
3
-
0
4
-
1
7
T
0
8
:
0
0
:

and so on.

And I would expect something similar to this:

    2013-04-17T08:00:00.001,41.14806,-9.58972,-13.0,0.0,0.0,-20.0,4
    2013-04-17T08:00:00.001,41.14806,-9.58972,-22.0,0.0,0.0,-20.0,4
    and so on

Any idea what's wrong?

4
  • possible duplicate of stackoverflow.com/questions/1433115/… Commented Apr 17, 2013 at 9:03
  • String **vec2[] is invalid identifier in Java ! Commented Apr 17, 2013 at 9:04
  • 2
    @NoobUnChained I think this is because he wanted it to be bolded. Commented Apr 17, 2013 at 9:08
  • Exactly, it was bolded, but someone edited to be in code format (and i'm thankful, because it's better to see) but that ** stayed there. Now it's correct. Thanks everybody for the answers, helped me a lot! Regards Commented Apr 17, 2013 at 9:15

7 Answers 7

10

You need to escape the |:

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

That's because the argument to split() is a regex not a string.

In regexes, some characters have special meanings. The vertical bar | represens alternation. So if you want to split according to |, you need to write \\| which like telling: "Don't take | as a special character, take it as the symbol |".

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

Comments

2

The argument to split is a regular expression and the "|" character has special meaning. Try escaping it \\|.

Comments

1

String.split(String) splits on a regular expression, not on a character. As you can see in the summary of Java regular expression constructs, the | functions as an or construct.

If you want to split on the | character, you might need to escape it using \|. Note that to escape it in a Java String, you'll need to escape the backslash as well: \\|.

Comments

1

The problem is that the split(String regex) takes a regular expression as argument. The pipe (|) is a special character in regex and must thus be escaped:

        String x = "2013-04-17T08:00:00.001,41.14806,-9.58972,-13.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-22.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-31.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-40.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-49.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-58.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-64.0,0.0,0.0,-20.0,4";
        String[] arr = x.split("\\|");
        for(String str : arr)
        {
            System.out.println(str);
        }

Yields:

2013-04-17T08:00:00.001,41.14806,-9.58972,-13.0,0.0,0.0,-20.0,4
2013-04-17T08:00:00.001,41.14806,-9.58972,-22.0,0.0,0.0,-20.0,4
2013-04-17T08:00:00.001,41.14806,-9.58972,-31.0,0.0,0.0,-20.0,4
2013-04-17T08:00:00.001,41.14806,-9.58972,-40.0,0.0,0.0,-20.0,4
2013-04-17T08:00:00.001,41.14806,-9.58972,-49.0,0.0,0.0,-20.0,4
2013-04-17T08:00:00.001,41.14806,-9.58972,-58.0,0.0,0.0,-20.0,4
2013-04-17T08:00:00.001,41.14806,-9.58972,-64.0,0.0,0.0,-20.0,4

Comments

1

Try this

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

Comments

1

You need to escape the | character, since it is the regex or pattern.

String x = "2013-04-17T08:00:00.001,41.14806,-9.58972,-13.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-22.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-31.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-40.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-49.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-58.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-64.0,0.0,0.0,-20.0,4";
String[] arr = x.split("\\|");
for(String s: arr){
    System.out.println(s);
}

Comments

0

did you try escaping the character as such

x.split("\\|");

1 Comment

You need to add an extra \ since the \ itself must also be escaped.

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.