Java:拆分包含特殊字符的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16297626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:12:50  来源:igfitidea点击:

Java : split a string that containing special characters

javaregexstring

提问by Dinoop paloli

I have a string like ||81|||01||and I want to split the string with |symbol.

我有一个像这样的字符串||81|||01||,我想用|符号分割字符串。

I had done this way,

我是这样做的,

String str = "||81|||01||";
System.out.println(str .split("\|").length); //printing 6 . But I am expecting 8

what is wrong with this code? | How can I split this string with that character so that I will get expected length (8)?;

这段代码有什么问题?| 我怎样才能用那个字符分割这个字符串,以便我得到预期的长度(8)?;

采纳答案by Maroun

Print:

打印:

System.out.println(Arrays.toString(str.split("\\|")));

System.out.println(Arrays.toString(str.split("\\|")));

And you'll understand why it's printing 6.

你就会明白为什么它打印 6。

You can try doing what you want using public String[] split(String regex, int limit):

您可以尝试使用 public String[] split(String regex, int limit)做您想做的事情:

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.

limit 参数控制应用模式的次数,因此会影响结果数组的长度。

So you should do:

所以你应该这样做:

System.out.println(str.split("\|", -1).length);

Now, printing the array will print:

现在,打印数组将打印:

[, , 81, , , 01, , ]as you expected.

[, , 81, , , 01, , ]正如你所料。

回答by Lucas

You need to use:

您需要使用:

str.split("\|", -1)

The second parameter is limit. From the javadoc:

第二个参数是limit。从javadoc:

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

limit 参数控制应用模式的次数,因此会影响结果数组的长度。如果限制 n 大于零,则该模式将最多应用 n - 1 次,数组的长度将不大于 n,并且数组的最后一个条目将包含最后一个匹配的分隔符之外的所有输入。如果 n 为非正数,则该模式将被应用尽可能多的次数,并且数组可以具有任意长度。如果 n 为零,则该模式将被应用尽可能多的次数,数组可以具有任意长度,并且将丢弃尾随的空字符串。

回答by Martin Ender

Using split("\\|")is the same as split("\\|", 0), where the limit parameter 0tells the function "omit trailing empty strings". So you are missing the last two empty strings. Use the two-argument version and supply a negative number to obtain all parts (even trailing empty ones):

Usingsplit("\\|")与 相同split("\\|", 0),其中 limit 参数0告诉函数“省略尾随空字符串”。所以你错过了最后两个空字符串。使用两个参数的版本并提供一个负数来获取所有部分(甚至尾随空部分):

str.split("\|", -1)

回答by javadev

str.split("\\|", -1)will do the necessary. Possible duplicate : Here

str.split("\\|", -1)会做必要的。可能重复:这里

回答by Datta

String str = "||81|||01||";System.out.println(str.split("\\|", 8).length);

String str = "||81|||01||";System.out.println(str.split("\\|", 8).length);

The second argument to split specifies maximum number of matches. Single argument split is like invoking split(str, 0) which leaves out trailing strings. See javadoc of both for more explaination.

split 的第二个参数指定最大匹配数。单个参数 split 就像调用 split(str, 0) 一样,它会忽略尾随字符串。有关更多解释,请参阅两者的 javadoc。

回答by Santosh

You can also use string.split(Pattern.quote("|"),-1) for spliting a string on a special character.

您还可以使用 string.split(Pattern.quote("|"),-1) 将字符串拆分为特殊字符。