2013-02-07 45 views
6

我在Java制作节目和所有的都很顺利,直到我想做一个while循环是这样的:怎么办=字符串

while(String.notEqual(Something)){...}

我知道有没有这样的不平等,但是有类似的东西?

回答

18

使用!句法。例如

if (!"ABC".equals("XYZ")) 
{ 
// do something 
} 
3

使用.equals结合的不!操作。从JLS §15.15.6

类型一元!操作者的操作数的表达式必须是 booleanBoolean,或编译时会出现误差。

一元逻辑补码表达式的类型是boolean

在运行时,如果需要 ,则操作数会经历拆箱转换(§5.1.8)。如果(可能转换的)操作数值是false,并且false(如果 (可能转换的)操作数值是true),则一元逻辑补码表达式的值是 true

0
while(!string.equals(Something)) 
{ 
    // Do some stuffs 
} 
2
String a = "hello"; 
String b = "nothello"; 
while(!a.equals(b)){...} 
1
String text1 = new String("foo"); 
String text2 = new String("foo"); 

while(text1.equals(text2)==false)//Comparing with logical no 
{ 
    //Other stuff... 
} 

while(!text1.equals(text2))//Negate the original statement 
{ 
    //Other stuff... 
} 
0

有没有这样的东西叫做notEquals所以如果你想否定使用!

while(!"something".equals(yourString){ 
//do something 
} 
1

如果你想区分大小写的比较使用equals(),否则,你可以使用equalsIgnoreCase()

String s1 = "a"; 
String s2 = "A"; 

s1.equals(s2); // false 

if(!s1.equals(s2)){ 
    // do something 
} 

s1.equalsIgnoreCase(s2); // true 

字符串比较的另一种方式,是为某些情况下有用的(例如排序)是使用compareTo返回0如果字符串相等,> 0如果S1> S2和< 0否则

if(s1.compareTo(s2) != 0){ // not equal 

} 

也有compareToIgnoreCase

0

如果你已经改变了你的循环中的字符串,最好考虑NULL条件。

while(something != null && !"constString".equals(something)){ 
    //todo... 
}