2014-09-27 74 views
0

我認爲這是一個簡單的問題,我現在只是點空白。 在Java中,我想知道一個字符串是否只包含一個字符,但是允許空格。 下面是我想要完成的。當且僅當一個字符串包含一個不包含空格的字符

" }" = True

"}" = True

" } }" = False

+0

Character.isWhiteSpace(Char)測試一個字符是否爲空格。 – Will 2014-09-27 23:05:36

回答

4

使用trim()除去開頭和結尾的空白。然後檢查結果字符串的長度。

+0

。謝謝 – Mozzie 2014-09-27 23:02:29

0

只需使用這個表達式:"\\s*.\\s*"

String regex = "\\s*.\\s*"; 
System.out.println(" }".matches(regex)); // prints true 
System.out.println("}".matches(regex)); // prints true 
System.out.println(" } }".matches(regex)); // prints false 
+0

正則表達式的一個簡單的問題?準確地說是 – Mozzie 2014-09-27 23:01:30

相關問題