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

Say you want to store the information about a number of pets in an array. Typical information that you could store for each pet (where relevant) would be • Breed of animal • Animal's name • Its birth date • Its sex • Whether it has been sterilised or not • When it is due for its next inoculation • When it last had its wings clipped For each type of pet (eg. dog, cat or bird) you would typically define a class to hold the relevant data. Note: You do not need to implement these classes. Just answer the following questions. 3.1.1 What would be the advantage of creating a superclass (eg. Pet) and declaring an array of Pet objects over simply using an array of Objects, storing each of the instances of the different pet classes (eg. Dog, Cat or Bird) in it? 3.1.2 Would you define Pet as a class or as an interface? Why? (2) (2)

1593


what is the purpose of using rmisecuritymanager in rmi?

779


Can substring create new object?

819


Can we increase array size dynamically in java?

740


Can we make the abstract methods static in java?

827


Is singleton thread safe in java?

769


What is command line argument

972


Can we serialize static variables in java?

882


State differences between C and Java?

911


what is a green thread? : Java thread

807


What is sortedmap interface?

792


What is exception in java?

779


whatis Home interface and Remoteinterface? with example?

1784


What lambda means?

774


How can I become a good programmer?

707