2013-10-17 50 views
0

我需要查看一个字符串是否在Java中不包含任何内容。这里是我的代码:检查字符串是否不包含字符

public class Decipher { 
    public static void main(String[] args) { 
     System.out.println("Opening..."); 
     System.out.println("Application Open"); 

     String s = "yyyd"; 
     if(s.contains("")){ 
      System.out.println("s contains Y"); 
      s = s.replace("y", "a"); 
      System.out.println(s); 
     } 

    } 

} 

我怎么才能知道它是否s不包含任何东西?

+0

阅读的javadoc。另外,'isEmpty()'。 –

+0

s.charAt(an_int)=='字符' – JNL

+0

s.contains(“”)对于非空字符串始终为真。为什么不:'s.equals(“”)'或者相反,'!s.equals(“”)'? – Cruncher

回答

0

如果要检查对一个空值,那么你可以使用

if (s != null) { 
    dosomething(); 
} 

如果你想核对空,实例化字符串,然后用

if (s.equals("") { 
    doSomethingElse(); 
} 

null字符串和空字符串是两个完全不同的东西。

-1

如果即时通讯正确,你想检查一个字符串是否为空? 最简单的方法是这样的

在(string == NULL)

,或者,如果你想检查一个字符串是否是空值或空白仅

如果( string.trim()== NULL)

+0

null和empty不是同一个东西 – redFIVE

1

你可以使用CommonsValidator -> GenericValidator

// returns true if 's' does not contain anything or is null 
GenericValidator.isBlankOrNull(s) 

而且不依赖于外部库

if (s == null || s.trim().length() == 0) { 
    // do your stuff 
} 
+0

也是org.apache.commons.lang.StringUtils,它包含了isBlank方法 – OutOfBound

相关问题