This question already has an answer here:
这个问题在这里已有答案:
- Java String split removed empty values 5 answers
- Java String拆分删除空值5个答案
I need help with the split()
method. I have the followingString
:
我需要split()方法的帮助。我有以下字符串:
String values = "0|0|0|1|||0|1|0|||";
I need to put the values into an array. There are 3 possible strings: "0", "1", and ""
我需要将值放入数组中。有3种可能的字符串:“0”,“1”和“”
My problem is, when i try to use split()
:
我的问题是,当我尝试使用split()时:
String[] array = values.split("\\|");
My values are saved only until the last 0. Seems like the part "|||" gets trimmed. What am i doing wrong?
我的值只保存到最后0.看起来像“|||”部分被修剪。我究竟做错了什么?
thanks
谢谢
4 个解决方案
#1
120
This behavior is explicitly documented in String.split(String regex)
(emphasis mine):
String.split(String regex)(强调我的)强调了这种行为:
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
此方法的作用就像通过调用给定表达式和limit参数为零的双参数split方法一样。因此,结尾的空字符串不包含在结果数组中。
If you want those trailing empty strings included, you need to use String.split(String regex, int limit)
with a negative value for the second parameter (limit
):
如果您希望包含那些尾随空字符串,则需要使用String.split(String regex,int limit),并为第二个参数(limit)使用负值:
String[] array = values.split("\\|", -1);
#2
23
Try this
尝试这个
String[] array = values.split("\\|",-1);
#3
7
Consider this example:
考虑这个例子:
public class StringSplit {
public static void main(String args[]) throws Exception{
String testString = "Real|How|To|||";
System.out.println
(java.util.Arrays.toString(testString.split("\\|")));
// output : [Real, How, To]
}
}
The result does not include the empty strings between the "|" separator. To keep the empty strings :
结果不包括“|”之间的空字符串分隔器。保持空字符串:
public class StringSplit {
public static void main(String args[]) throws Exception{
String testString = "Real|How|To|||";
System.out.println
(java.util.Arrays.toString(testString.split("\\|", -1)));
// output : [Real, How, To, , , ]
}
}
For more details go to this website: http://www.rgagnon.com/javadetails/java-0438.html
有关更多详细信息,请访问以下网站:http://www.rgagnon.com/javadetails/java-0438.html
#4
1
This is expected. Refer to Javadocs for split
.
这是预料之中的。请参阅Javadocs以进行拆分。
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split(java.lang.String,int) method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
此方法的工作方式就像通过调用给定表达式和limit参数为零的双参数split(java.lang.String,int)方法一样。因此,结尾的空字符串不包含在结果数组中。
#1
120
This behavior is explicitly documented in String.split(String regex)
(emphasis mine):
String.split(String regex)(强调我的)强调了这种行为:
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
此方法的作用就像通过调用给定表达式和limit参数为零的双参数split方法一样。因此,结尾的空字符串不包含在结果数组中。
If you want those trailing empty strings included, you need to use String.split(String regex, int limit)
with a negative value for the second parameter (limit
):
如果您希望包含那些尾随空字符串,则需要使用String.split(String regex,int limit),并为第二个参数(limit)使用负值:
String[] array = values.split("\\|", -1);
#2
23
Try this
尝试这个
String[] array = values.split("\\|",-1);
#3
7
Consider this example:
考虑这个例子:
public class StringSplit {
public static void main(String args[]) throws Exception{
String testString = "Real|How|To|||";
System.out.println
(java.util.Arrays.toString(testString.split("\\|")));
// output : [Real, How, To]
}
}
The result does not include the empty strings between the "|" separator. To keep the empty strings :
结果不包括“|”之间的空字符串分隔器。保持空字符串:
public class StringSplit {
public static void main(String args[]) throws Exception{
String testString = "Real|How|To|||";
System.out.println
(java.util.Arrays.toString(testString.split("\\|", -1)));
// output : [Real, How, To, , , ]
}
}
For more details go to this website: http://www.rgagnon.com/javadetails/java-0438.html
有关更多详细信息,请访问以下网站:http://www.rgagnon.com/javadetails/java-0438.html
#4
1
This is expected. Refer to Javadocs for split
.
这是预料之中的。请参阅Javadocs以进行拆分。
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split(java.lang.String,int) method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
此方法的工作方式就像通过调用给定表达式和limit参数为零的双参数split(java.lang.String,int)方法一样。因此,结尾的空字符串不包含在结果数组中。