How do you compare two strings? any predefined method for
this?
Answers were Sorted based on User's Feedback
You can compare the values of 2 Strings by following
methods of String class:-
Eg:- String a="abc"; String b="abc";
1> .equals() method it will return a boolean value.
eg:- if(a.equals(b)) will return true.
2> .compareTo() method.If the two java strings are exactly
the same, the compareTo method will return a value of 0
(zero).
eg:- if (a.compareTo(b) == 0){
// this line will print
System.out.println("a and b strings are the same.")
}
| Is This Answer Correct ? | 4 Yes | 0 No |
What is hashing principle in java?
What is meant by stack and queue?
How to implement a multithreaded applet?
Why do we need singleton?
How to create com object in Java?
What are the 4 versions of java?
What does ide stand for?
What is thread pool in java with example?
String and stringbuffer both represent string objects. Can we compare string and stringbuffer in java?
Explain cookies?
what is polymorphism?
Why to use nested classes in java? (Or) what is the purpose of nested class in java?