Java字符串分割为"。”(点)(复制)

时间:2022-03-15 01:41:59

This question already has an answer here:

这个问题已经有了答案:

Why second line of this code in Java throws ArrayIndexOutOfBoundsException?

为什么Java中这段代码的第二行会抛出ArrayIndexOutOfBoundsException?

String filename = "D:/some folder/001.docx";
String extensionRemoved = filename.split(".")[0];

While below works:

而下面的工作:

String driveLetter = filename.split("/")[0];

I use Java 7.

我使用Java 7。

4 个解决方案

#1


552  

You need to escape the dot if you want to split on a literal dot:

如果你想在文字点上分裂,你需要逃离这个点:

String extensionRemoved = filename.split("\\.")[0];

Otherwise you are splitting on the regex ., which means "any character".
Note the double backslash needed to create a single backslash in the regex.

否则,您将在regex上分裂,这意味着“任何字符”。注意在regex中创建单个反斜杠所需的双反斜杠。


You're getting an ArrayIndexOutOfBoundsException because your input string is just a dot, ie ".", which is an edge case that produces an empty array when split on dot; split(regex) removes all trailing blanks from the result, but since splitting a dot on a dot leaves only two blanks, after trailing blanks are removed you're left with an empty array.

你会得到ArrayIndexOutOfBoundsException,因为你的输入字符串只是一个点。,当在点上分割时产生空数组的边缘情况;split(regex)将从结果中删除所有尾随空格,但是由于在点上分割一个点只留下两个空格,所以在删除尾随空格之后,您将得到一个空数组。

To avoid getting an ArrayIndexOutOfBoundsException for this edge case, use the overloaded version of split(regex, limit), which has a second parameter that is the size limit for the resulting array. When limit is negative, the behaviour of removing trailing blanks from the resulting array is disabled:

为了避免在此边缘情况下获得ArrayIndexOutOfBoundsException,请使用已重载的split版本(regex, limit),它有第二个参数,即结果数组的大小限制。当极限为负值时,从结果数组中删除尾随空格的行为被禁用:

".".split("\\.", -1) // returns an array of two blanks, ie ["", ""]

ie, when filename is just a dot ".", calling filename.split("\\.", -1)[0] will return a blank, but calling filename.split("\\.")[0] will throw an ArrayIndexOutOfBoundsException.

当文件名只是一个点。",称filename.split(“\ \。[0]将返回一个空白,但是调用filename.split("\ . \.")[0]将抛出ArrayIndexOutOfBoundsException。

#2


54  

"." is a special character in java, so you have to use "\\." to escape this character:

“.”是java中的一个特殊字符,所以您必须使用“\\”来避免这个字符:

final String extensionRemoved = filename.split("\\.")[0];

I hope this helps

我希望这有助于

#3


30  

This is because . is a reserved character in regular expression, representing any character. Instead, we should use the following statement:

这是因为。是正则表达式中的保留字符,表示任何字符。相反,我们应该使用以下语句:

String extensionRemoved = filename.split("\\.")[0];

#4


16  

I believe you should escape the dot. Try:

我认为你应该避开这个点。试一试:

String filename = "D:/some folder/001.docx";
String extensionRemoved = filename.split("\\.")[0];

Otherwise dot is interpreted as any character in regular expressions.

否则,点将被解释为正则表达式中的任何字符。

#1


552  

You need to escape the dot if you want to split on a literal dot:

如果你想在文字点上分裂,你需要逃离这个点:

String extensionRemoved = filename.split("\\.")[0];

Otherwise you are splitting on the regex ., which means "any character".
Note the double backslash needed to create a single backslash in the regex.

否则,您将在regex上分裂,这意味着“任何字符”。注意在regex中创建单个反斜杠所需的双反斜杠。


You're getting an ArrayIndexOutOfBoundsException because your input string is just a dot, ie ".", which is an edge case that produces an empty array when split on dot; split(regex) removes all trailing blanks from the result, but since splitting a dot on a dot leaves only two blanks, after trailing blanks are removed you're left with an empty array.

你会得到ArrayIndexOutOfBoundsException,因为你的输入字符串只是一个点。,当在点上分割时产生空数组的边缘情况;split(regex)将从结果中删除所有尾随空格,但是由于在点上分割一个点只留下两个空格,所以在删除尾随空格之后,您将得到一个空数组。

To avoid getting an ArrayIndexOutOfBoundsException for this edge case, use the overloaded version of split(regex, limit), which has a second parameter that is the size limit for the resulting array. When limit is negative, the behaviour of removing trailing blanks from the resulting array is disabled:

为了避免在此边缘情况下获得ArrayIndexOutOfBoundsException,请使用已重载的split版本(regex, limit),它有第二个参数,即结果数组的大小限制。当极限为负值时,从结果数组中删除尾随空格的行为被禁用:

".".split("\\.", -1) // returns an array of two blanks, ie ["", ""]

ie, when filename is just a dot ".", calling filename.split("\\.", -1)[0] will return a blank, but calling filename.split("\\.")[0] will throw an ArrayIndexOutOfBoundsException.

当文件名只是一个点。",称filename.split(“\ \。[0]将返回一个空白,但是调用filename.split("\ . \.")[0]将抛出ArrayIndexOutOfBoundsException。

#2


54  

"." is a special character in java, so you have to use "\\." to escape this character:

“.”是java中的一个特殊字符,所以您必须使用“\\”来避免这个字符:

final String extensionRemoved = filename.split("\\.")[0];

I hope this helps

我希望这有助于

#3


30  

This is because . is a reserved character in regular expression, representing any character. Instead, we should use the following statement:

这是因为。是正则表达式中的保留字符,表示任何字符。相反,我们应该使用以下语句:

String extensionRemoved = filename.split("\\.")[0];

#4


16  

I believe you should escape the dot. Try:

我认为你应该避开这个点。试一试:

String filename = "D:/some folder/001.docx";
String extensionRemoved = filename.split("\\.")[0];

Otherwise dot is interpreted as any character in regular expressions.

否则,点将被解释为正则表达式中的任何字符。