2013-09-23 71 views
1

如何檢查字符串是否包含空格以外的任何內容?
此代碼不工作:android:檢查字符串是否不僅有空格

String string = " \n\n\t\t  "; 
if(string.length()==0) doSomething(); 

因爲空間和新的生產線具有值。

誰能告訴我怎麼辦?

注意minSDKVersion = 5

問候:)

回答

14

試試這個:

if (string.trim().length() == 0) { /* all white space */ } 

或者,您可以使用正則表達式:

if (string.matches("\\w*")) { . . . } 
+0

thanx它的工作 –

2

您可以使用trim從字符串的兩端刪除空白。然後與空字符串進行比較。

+0

感謝名單:)工作。 –

5

嘗試:

if (string == null || TextUtils.isEmpty(string.trim()) doSomething(); 
相關問題