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
How can you say java is object oriented?
How are variables stored?
What is the difference between a choice and a list?
Differentiate storage classes on the basis of their scope?
How to display arraylist values in java?
What is difference between throw and throws ?
What is lambda expression in java?
Can you override a final method?
What about abstract classes in java?
What is r in java?
what is mutual exclusion? How can you take care of mutual exclusion using java threads? : Java thread
What language is an assembler written in?
How many arguments can be passed to main ()?
Which access specifier can be used with class ?
Explain the reason behind ending a program with a system.exit(0)?