简体   繁体   中英

Java string split giving unexpected result

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?

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 | ".

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

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: \\\\| .

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

尝试这个

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

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);
}

你试过逃避这个角色吗?

x.split("\\|");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM