我試圖根據單詞之間的空格拆分輸入句子。它沒有按預期工作。如何使用掃描程序分隔空格作爲分隔符的字符串
public static void main(String[] args) {
Scanner scaninput=new Scanner(System.in);
String inputSentence = scaninput.next();
String[] result=inputSentence.split("-");
// for(String iter:result) {
// System.out.println("iter:"+iter);
// }
System.out.println("result.length: "+result.length);
for (int count=0;count<result.length;count++) {
System.out.println("==");
System.out.println(result[count]);
}
}
它給下面當我使用輸出「 - 」在分裂:
fsfdsfsd-second-third
result.length: 3
==
fsfdsfsd
==
second
==
third
當我替換「 - 」有空間「」,它給下面的輸出。
first second third
result.length: 1
==
first
有關這裏有什麼問題的任何建議?我已經提到了過帳流程How to split a String by space,但它不起作用。
使用split("\\s+")
給出了這樣的輸出:
first second third
result.length: 1
==
first
如果有多個空格,請嘗試使用'\\ s +'。告訴我們你究竟試過了什麼*完全*。 – TheLostMind 2015-03-13 06:24:32
這是輸出的第一個第二個第三個 result.length:1 == 第一個 – Zack 2015-03-13 06:25:18
在我的問題中添加了相同的更好的視圖。無法在評論中格式化 – Zack 2015-03-13 06:26:38