java 如何拆分仅包含分隔符的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/785586/
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
How can split a string which contains only delimiter?
提问by Michael Myers
I am using the following code:
我正在使用以下代码:
String sample = "::";
String[] splitTime = sample.split(":");
// extra detail omitted
System.out.println("Value 1 :"+splitTime[0]);
System.out.println("Value 2 :"+splitTime[1]);
System.out.println("Value 3 :"+splitTime[2]);
I am getting ArrayIndexOutofBoundexception. How does String.split()handle consecutive or trailing / opening delimiters?
我得到了ArrayIndexOutofBound例外。如何String.split()处理连续或尾随/开始分隔符?
See also:
另见:
回答by Michael Myers
Alnitak is correct that trailing empty strings will be discarded by default.
Alnitak 是正确的,默认情况下将丢弃尾随的空字符串。
If you want to have trailing empty strings, you should use split(String, int)and pass a negative number as the limitparameter.
如果您想要尾随空字符串,您应该使用split(String, int)并传递一个负数作为limit参数。
The
limitparameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit nis 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 nis non-positive then the pattern will be applied as many times as possible and the array can have any length. If nis 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为零,则该模式将被应用尽可能多的次数,数组可以具有任意长度,并且将丢弃尾随的空字符串。
Note that split(aString)is a synonym for split(aString, 0):
请注意,这split(aString)是 的同义词split(aString, 0):
This method works as if by invoking the two-argument
splitmethod with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
此方法的工作方式就像通过
split使用给定表达式和零限制参数调用双参数方法一样。因此,结果数组中不包含尾随空字符串。
Also, you should use a loop to get the values from the array; this avoids a possible ArrayIndexOutOfBoundsException.
此外,您应该使用循环从数组中获取值;这避免了可能的ArrayIndexOutOfBoundsException.
So your corrected code should be (assuming you wantthe trailing empty strings):
所以你更正的代码应该是(假设你想要尾随的空字符串):
String sample = "::";
String[] splitTime = sample.split(":", -1);
for (int i = 0; i < splitTime.length; i++) {
System.out.println("Value " + i + " : \"" + splitTime[i] + "\"");
}
Output:
输出:
Value 0 : "" Value 1 : "" Value 2 : ""
回答by Alnitak
From the J2SE API manual:
Trailing empty strings are therefore not included in the resulting array.
因此,结果数组中不包含尾随空字符串。
So, if you pass in "::" you'll get an empty array because all of the delimiters are trailing.
所以,如果你传入“ ::”,你会得到一个空数组,因为所有的分隔符都在尾随。
If you want to make sure that you get no morethan three entries you should use:
如果你想确保你得到的条目不超过三个,你应该使用:
String[] splitTime = sample.split(":", 3);
With an input of "::" that would indeed give you three empty strings in the output array.
输入“ ::”确实会在输出数组中为您提供三个空字符串。
However if the input only happens to have one ":" in it then you'll still only get two elements in your array.
但是,如果输入中恰好只有一个“ :”,那么您仍然只能在数组中获得两个元素。
回答by Matthew Sposato
Like this perhaps?
也许像这样?
int ndx = 0;
StringTokenizer t = new StringTokenizer(": : ::::",":");
while (t.hasMoreElements())
{
System.out.println(String.format("Value %d : %s", ++ndx,t.nextElement()));
}
回答by user53378
you should check the length of the splitTime array.
您应该检查 splitTime 数组的长度。
回答by Jugal
Use the function StringTokenizer in which u pass the string and the second argument as delimiter
使用函数 StringTokenizer 在其中传递字符串和第二个参数作为分隔符
use splittime.length function to find the length
使用 splittime.length 函数查找长度

