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


Please Help Members By Posting Answers For Below Questions

How can you say java is object oriented?

831


How are variables stored?

742


What is the difference between a choice and a list?

842


Differentiate storage classes on the basis of their scope?

880


How to display arraylist values in java?

731


What is difference between throw and throws ?

867


What is lambda expression in java?

727


Can you override a final method?

792


What about abstract classes in java?

838


What is r in java?

790


what is mutual exclusion? How can you take care of mutual exclusion using java threads? : Java thread

904


What language is an assembler written in?

765


How many arguments can be passed to main ()?

755


Which access specifier can be used with class ?

729


Explain the reason behind ending a program with a system.exit(0)?

820