Java中的split()方法不能在点(.)[duplicate]上工作。

时间:2021-04-06 21:39:27

This question already has an answer here:

这个问题已经有了答案:

I have prepared a simple code snippet in order to separate the erroneous portion from my web application.

我已经编写了一个简单的代码片段,以便将错误的部分从web应用程序中分离出来。

public class Main {

    public static void main(String[] args) throws IOException {
        System.out.print("\nEnter a string:->");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String temp = br.readLine();

        String words[] = temp.split(".");

        for (int i = 0; i < words.length; i++) {
            System.out.println(words[i] + "\n");
        }
    }
}

I have tested it while building a web application JSF. I just want to know why in the above code temp.split(".") does not work. The statement,

我在构建web应用程序JSF时对它进行了测试。我只是想知道为什么在上面的代码temp.split(".")不起作用。声明,

System.out.println(words[i]+"\n"); 

displays nothing on the console means that it doesn't go through the loop. When I change the argument of the temp.split() method to other characters, It works just fine as usual. What might be the problem?

在控制台上没有显示任何东西意味着它不经过循环。当我将temp.split()方法的参数更改为其他字符时,它可以正常工作。有什么问题吗?

7 个解决方案

#1


370  

java.lang.String.split splits on regular expressions, and . in a regular expression means "any character".

以。对正则表达式进行分割。在正则表达式中,意思是“任何字符”。

Try temp.split("\\.").

尝试temp.split(“\ \”。)。

#2


56  

The documentation on split() says:

关于split()的文档说:

Splits this string around matches of the given regular expression.

将这个字符串分割为给定正则表达式的匹配项。

(Emphasis mine.)

(强调我的。)

A dot is a special character in regular expression syntax. Use Pattern.quote() on the parameter to split() if you want the split to be on a literal string pattern:

点是正则表达式语法中的一个特殊字符。如果您希望将split()设置为文字字符串模式,请在要分割的参数上使用pattern .quote():

String[] words = temp.split(Pattern.quote("."));

#3


10  

The method takes a regular expression, not a string, and the dot has a special meaning in regular expressions. Escape it like so split("\\."). You need a double backslash, the second one escapes the first.

该方法采用正则表达式,而不是字符串,而点在正则表达式中具有特殊的意义。像分裂一样逃离它(“\\”)。你需要一个双反斜杠,第二个转义第一个。

#4


9  

Try:

试一试:

String words[]=temp.split("\\.");

The method is:

方法是:

String[] split(String regex) 

"." is a reserved char in regex

“。”是regex中的保留字符。

#5


5  

\\. is the simple answer. Here is simple code for your help.

\ \。是一个简单的答案。这是你的帮助的简单代码。

while (line != null) {
    //             
    String[] words = line.split("\\.");
    wr = "";
    mean = "";
    if (words.length > 2) {
        wr = words[0] + words[1];
        mean = words[2];

    } else {
        wr = words[0];
        mean = words[1];
    }
}

#6


2  

    private String temp = "mahesh.hiren.darshan";

    String s_temp[] = temp.split("[.]");

  Log.e("1", ""+s_temp[0]);

#7


2  

It works fine. Did you read the documentation? The string is converted to a regular expression.

它将正常工作。你看过文件了吗?字符串被转换为正则表达式。

. is the special character matching all input characters.

。是匹配所有输入字符的特殊字符。

As with any regular expression special character, you escape with a \. You need an additional \ for the Java string escape.

与任何正则表达式特殊字符一样,您使用一个\来转义。Java字符串转义需要一个额外的\。

#1


370  

java.lang.String.split splits on regular expressions, and . in a regular expression means "any character".

以。对正则表达式进行分割。在正则表达式中,意思是“任何字符”。

Try temp.split("\\.").

尝试temp.split(“\ \”。)。

#2


56  

The documentation on split() says:

关于split()的文档说:

Splits this string around matches of the given regular expression.

将这个字符串分割为给定正则表达式的匹配项。

(Emphasis mine.)

(强调我的。)

A dot is a special character in regular expression syntax. Use Pattern.quote() on the parameter to split() if you want the split to be on a literal string pattern:

点是正则表达式语法中的一个特殊字符。如果您希望将split()设置为文字字符串模式,请在要分割的参数上使用pattern .quote():

String[] words = temp.split(Pattern.quote("."));

#3


10  

The method takes a regular expression, not a string, and the dot has a special meaning in regular expressions. Escape it like so split("\\."). You need a double backslash, the second one escapes the first.

该方法采用正则表达式,而不是字符串,而点在正则表达式中具有特殊的意义。像分裂一样逃离它(“\\”)。你需要一个双反斜杠,第二个转义第一个。

#4


9  

Try:

试一试:

String words[]=temp.split("\\.");

The method is:

方法是:

String[] split(String regex) 

"." is a reserved char in regex

“。”是regex中的保留字符。

#5


5  

\\. is the simple answer. Here is simple code for your help.

\ \。是一个简单的答案。这是你的帮助的简单代码。

while (line != null) {
    //             
    String[] words = line.split("\\.");
    wr = "";
    mean = "";
    if (words.length > 2) {
        wr = words[0] + words[1];
        mean = words[2];

    } else {
        wr = words[0];
        mean = words[1];
    }
}

#6


2  

    private String temp = "mahesh.hiren.darshan";

    String s_temp[] = temp.split("[.]");

  Log.e("1", ""+s_temp[0]);

#7


2  

It works fine. Did you read the documentation? The string is converted to a regular expression.

它将正常工作。你看过文件了吗?字符串被转换为正则表达式。

. is the special character matching all input characters.

。是匹配所有输入字符的特殊字符。

As with any regular expression special character, you escape with a \. You need an additional \ for the Java string escape.

与任何正则表达式特殊字符一样,您使用一个\来转义。Java字符串转义需要一个额外的\。