【发布时间】:2017-02-26 01:05:53
【问题描述】:
我编写了这个简单的程序,它在每个非数字字符处分割给定的输入。
public class Fileread {
public static void main(String[] args) throws IOException {
//Declarations
String[] temp;
String current;
//Execution
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
while ((current = br.readLine()) != null) {
temp = current.split("\\D"); //Splitting at Non Digits
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
}
}
}
}
这是 input.txt :
hello1world2
world3
end4of5world6
输出:
1
2
3
4
5
6
为什么会出现这么多多余的空格?我需要在单独的行上打印每个数字,中间没有空格。我该如何解决这个问题?
【问题讨论】:
-
使用
\\D+模式。但是,如果您的字符串以非数字开头,则可能仍会保留前导空元素。