拆分Java String返回空数组? [重复]

时间:2021-02-19 18:08:32

This question already has an answer here:

这个问题在这里已有答案:

I have a String something like this

我有一个像这样的字符串

"myValue"."Folder"."FolderCentury";

I want to split from dot("."). I was trying with the below code:

我想从点(“。”)拆分。我正在尝试使用以下代码:

String a = column.replace("\"", "");
String columnArray[] = a.split(".");

But columnArray is coming empty. What I am doing wrong here?

但是columnArray将变空。我在这做错了什么?

I will want to add one more thing here someone its possible String array object will contain spitted value like mentioned below only two object rather than three.?

我想在这里再添加一个东西,其可能的String数组对象将包含spitted值,如下面提到的只有两个对象而不是三个。?

columnArray[0]= "myValue"."Folder";
columnArray[1]= "FolderCentury";

5 个解决方案

#1


27  

Note that String#split takes a regex.

请注意,String#split采用正则表达式。

You need to escape the special char . (That means "Any character"):

你需要逃避特殊的字符。 (这意味着“任何角色”):

 String columnArray[] = a.split("\\.");

(Escaping a regex is done by \, but in Java, \ is written as \\).

(转义正则表达式由\完成,但在Java中,\写为\\)。

You can also use Pattern#quote:

你也可以使用Pattern#quote:

Returns a literal pattern String for the specified String.

返回指定String的文字模式String。

String columnArray[] = a.split(Pattern.quote("."));

String columnArray [] = a.split(Pattern.quote(“。”));

By escaping the regex, you tell the compiler to treat the . as the String . and not the special char ..

通过转义正则表达式,您告诉编译器处理。作为字符串。而不是特殊的炭..

#2


3  

You must escape the dot.

你必须逃脱点。

String columnArray[] = a.split("\\.");

#3


2  

split() accepts an regular expression. So you need to skip '.' to not consider it as a regex meta character.

split()接受正则表达式。所以你需要跳过'。'不要将它视为正则表达式元字符。

String[] columnArray = a.split("\\."); 

#4


1  

While using special characters need to use the particular escape sequence with it.

使用特殊字符时需要使用特定的转义序列。

'.' is a special character so need to use escape sequence before '.' like:

''是一个特殊字符,所以需要在'。'之前使用转义序列。喜欢:

 String columnArray[] = a.split("\\.");

#5


1  

The next code:

下一个代码:

   String input = "myValue.Folder.FolderCentury";
   String regex = "(?!(.+\\.))\\.";
   String[] result=input.split(regex);
   System.out.println(Arrays.toString(result));

Produces the required output:

产生所需的输出:

[myValue.Folder, FolderCentury]

The regular Expression tweaks a little with negative look-ahead (this (?!) part), so it will only match the last dot on a String with more than one dot.

正则表达式稍微调整一下,使用负向前瞻(这个(?!)部分),因此它只匹配具有多个点的字符串上的最后一个点。

#1


27  

Note that String#split takes a regex.

请注意,String#split采用正则表达式。

You need to escape the special char . (That means "Any character"):

你需要逃避特殊的字符。 (这意味着“任何角色”):

 String columnArray[] = a.split("\\.");

(Escaping a regex is done by \, but in Java, \ is written as \\).

(转义正则表达式由\完成,但在Java中,\写为\\)。

You can also use Pattern#quote:

你也可以使用Pattern#quote:

Returns a literal pattern String for the specified String.

返回指定String的文字模式String。

String columnArray[] = a.split(Pattern.quote("."));

String columnArray [] = a.split(Pattern.quote(“。”));

By escaping the regex, you tell the compiler to treat the . as the String . and not the special char ..

通过转义正则表达式,您告诉编译器处理。作为字符串。而不是特殊的炭..

#2


3  

You must escape the dot.

你必须逃脱点。

String columnArray[] = a.split("\\.");

#3


2  

split() accepts an regular expression. So you need to skip '.' to not consider it as a regex meta character.

split()接受正则表达式。所以你需要跳过'。'不要将它视为正则表达式元字符。

String[] columnArray = a.split("\\."); 

#4


1  

While using special characters need to use the particular escape sequence with it.

使用特殊字符时需要使用特定的转义序列。

'.' is a special character so need to use escape sequence before '.' like:

''是一个特殊字符,所以需要在'。'之前使用转义序列。喜欢:

 String columnArray[] = a.split("\\.");

#5


1  

The next code:

下一个代码:

   String input = "myValue.Folder.FolderCentury";
   String regex = "(?!(.+\\.))\\.";
   String[] result=input.split(regex);
   System.out.println(Arrays.toString(result));

Produces the required output:

产生所需的输出:

[myValue.Folder, FolderCentury]

The regular Expression tweaks a little with negative look-ahead (this (?!) part), so it will only match the last dot on a String with more than one dot.

正则表达式稍微调整一下,使用负向前瞻(这个(?!)部分),因此它只匹配具有多个点的字符串上的最后一个点。