Java 中的 split() 方法对点 (.) [英] The split() method in Java does not work on a dot (.)
问题描述
我准备了一个简单的代码片段,以便将错误的部分与我的网络应用程序分开.
I have prepared a simple code snippet in order to separate the erroneous portion from my web application.
public class Main {
public static void main(String[] args) throws IOException {
System.out.print("
Enter 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] + "
");
}
}
}
我在构建 Web 应用程序 JSF 时对其进行了测试.我只是想知道为什么在上面的代码中 temp.split(".")
不起作用.声明,
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,
System.out.println(words[i]+"
");
在控制台上不显示任何内容意味着它没有通过循环.当我将 temp.split()
方法的参数更改为其他字符时,它照常工作.可能是什么问题?
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?
推荐答案
java.lang.String.split
在正则表达式上拆分,而 .
在正则表达式中表示任何字符".
java.lang.String.split
splits on regular expressions, and .
in a regular expression means "any character".
试试temp.split("\.")
.
这篇关于Java 中的 split() 方法对点 (.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!