How do you compare two strings? any predefined method for
this?

Answers were Sorted based on User's Feedback



How do you compare two strings? any predefined method for this?..

Answer / niranjanravi

equals() method

Is This Answer Correct ?    8 Yes 2 No

How do you compare two strings? any predefined method for this?..

Answer / guest

equals() method

Is This Answer Correct ?    6 Yes 2 No

How do you compare two strings? any predefined method for this?..

Answer / 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

More Core Java Interview Questions

Why are functions called methods in java?

0 Answers  


What is the new line character?

0 Answers  


What are the different http methods?

0 Answers  


what are the oops concept in java explain with real time examples

24 Answers   Accenture, Bosch, Consummate Technologies, CTS, Current Technologies, iNautix, Infosys, Kekran Mekran, QA InfoTech, RTRT, SofTech, Tech Mahindra, Thorogood, Timios, Wipro,


Name the method that is used to set a TextComponent to the read-only state?

1 Answers  






What is vector capacity in java?

0 Answers  


________ exception must be either caught or specified in throws class of the method.

1 Answers  


What does || || mean in math?

0 Answers  


Explain the difference between association, aggregation and inheritance relationships.

0 Answers  


public class AboutStrings{ public static void main(String args[]){ String s1="hello"; String s2="hel"; String s3="lo"; String s4=s2+s3; //to know the hash codes of s1,s4. System.out.println(s1.hashCode()); System.out.println(s4.hashCode()); // these two s1 and s4 are having same hashcodes. if(s1==s4){ System.out.println("s1 and s4 are same."); }else System.out.println("s1 and s4 are not same."); } } Somebody told me that, == operator compares references of the objects. In the above example even though s1 and s4 are refering to same object(having same hash codes), it is printing s1 and s4 are not same. Can anybody explain in detail why it is behaving like this? Thanks in Advance RavuriVinod

4 Answers   TCS,


What is the difference between replace and replace all?

0 Answers  


What does this () mean in constructor chaining concept?

0 Answers  


Categories