+ 1
Can I compare two strings using 'if' statement?
6 Antworten
+ 10
You can compare string using .equals method. For example.
String a = "a";
String b = "b";
if(a.equals(b)){
System.out.println("a equals b");
}
+ 2
Use (str1.compare(str2) > 0) to compare Strings. compare returns -1 if str1 < str2. compare returns 0 if str1 is equals str2. And compare returns +1 if str1 > str2. Always check s1.compare(s2) with Zero, like: < 0, or > 0, or == 0.
+ 1
yes, you can compare the two string using the logical operator (||,&&).
ex
"string1" equal .(string)|| "string" equal .(string)
0
Yes, several options, case sensitive or case insensitive string comparisons, in java == can be used but with a grain of salt, several methods are available for string comparisons, i suggest you look into those, they're safer.
0
u can use if statement to compare contents and reference of two string
- 1
Yes, but you can't use OR(||) and AND(&&). To use || or && you need equals:
"String1" equals.(String) || "String2" equals.(String)