+ 1

Can I compare two strings using 'if' statement?

20th Jul 2016, 7:29 AM
KIRAN JOHNSON
KIRAN JOHNSON - avatar
6 odpowiedzi
+ 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"); }
20th Jul 2016, 8:52 AM
WPimpong
WPimpong - avatar
+ 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.
22nd Jul 2016, 11:42 PM
gorlok
+ 1
yes, you can compare the two string using the logical operator (||,&&). ex "string1" equal .(string)|| "string" equal .(string)
28th Nov 2016, 7:50 PM
Ananta Khare
Ananta Khare - avatar
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.
21st Jul 2016, 4:46 AM
Eric Gitangu
Eric Gitangu - avatar
0
u can use if statement to compare contents and reference of two string
6th Nov 2016, 7:02 PM
ANAND ALABAL
ANAND ALABAL - avatar
- 1
Yes, but you can't use OR(||) and AND(&&). To use || or && you need equals: "String1" equals.(String) || "String2" equals.(String)
20th Jul 2016, 8:57 AM
Estêvão Rolim
Estêvão Rolim - avatar