简体   繁体   中英

Java String.split(“\\s+”)

I am trying to parse some output. The output is as follows:

raidz1-0 ONLINE 0 0 0

My code is as follows:

String line = "\traidz1-0  ONLINE       0     0     0";
String[] tokens = line.split("\\s+");

tokens ends up being {"raidz1-0", "ONLINE", "0"}

For some reason the last 2 zeros are discarded. I would like to retain the zeros, please tell me how.

This code:

String line = "\traidz1-0  ONLINE       0     0     0";
String[] tokens = line.split("\\s+");
System.out.println(tokens.length);
System.out.println(Arrays.toString(tokens));

prints:

    6
    [, raidz1-0, ONLINE, 0, 0, 0]

Well, it works fine in my case, and it should work: -

String line = "\traidz1-0  ONLINE       0     0     0";
String[] tokens = line.split("\\s+");

System.out.println(Arrays.toString(tokens));

OUTPUT : -

[, raidz1-0, ONLINE, 0, 0, 0]

Are you sure, you didn't get the last two zeros?

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