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
Why do we use threads in java?
What is a 16 bit word?
Can we execute a program without main?
How a string is stored in memory?
What are nested classes in java?
What is the difference between char and char *?
How does system arraycopy work in java?
What is the relationship difference the canvas class and the graphics class?
What do you mean by scope of variable?
What is a parameter in a function?
What are the differences between string and stringbuffer?
What is subsequence of a string?
What methodology can be employed to locate substrings inside a string?
Explain the importance of finally block in java?
Can we use switch statement with strings?