How do you compare two strings? any predefined method for
this?
Answer Posted / sumann
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 |
Post New Answer View All Answers
What is udp in java?
difference between byte stream class and character stream class?
Which is better stringbuffer or stringbuilder?
What are 5 boolean operators?
What does nextint () do in java?
Can we have multiple classes in a single file?
What is compareto?
What are recursive functions?
what is recursion in java
What is the java virtual machine?
What java is used for?
What is static in java?
Can we override final method?
Does windows 10 need java?
Enlist diffrent types of inheritance supported by java?