2013-09-23 65 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(); 
相关问题