How to compare string using java program regardless of their case?
Hello to all,
I am new to this forum. I recently started learning java language. In our assignment there is one program like How to compare string using java program regardless of their case? I tried various method but none of them worked out. Can anyone tell me how to solve this program.
Thank you.
Re: How to compare string using java program regardless of their case?
I have written following program for you. In this following program I use String.equalsIgnoreCase method to compare two string equality regarding it case. It is very easy process. It use ASCII value of string and use this value to compare 2 string. Just try to understand following program.
Code:
package om.techarena.example.lang;
public class EgIgnoreCase {
public static void main(String[] args) {
String upper1 = "xyzcvs";
String mixed1 = "xYZCvS";
if (upper1.equalsIgnoreCase(mixed1)) {
System.out.println("Upper1 and Mixed1 equals.");
}
}
}
Re: How to compare string using java program regardless of their case?
You have to use following code in your program to compare two strings regardless of their case. It is very simple program. You have to just take two string from user and then you have to use If statement to compare two values. You can also use boolean data type for true or false value.
Code:
int s1, s2;
s1 = Name1.length();
s2 = Name2.length();
if ( s1 ==s2){
boolean state = true;
}
else {
boolean state = false;
}
Re: How to compare string using java program regardless of their case?
I have written following program for you. In this program I have use equals() method to compare one string with another. It is very easy process. You have to just compare first string with another. If both are same then you will get positive result otherwise you will get negative result.
Code:
String str1 = new String("ROMAN");
String str2 = new String("roman");
if (str1.equals(str2)) {
System.out.println("The two strings are equal.");
}
else {
System.out.println("The two strings are not
equal.");
}
Re: How to compare string using java program regardless of their case?
Hey it is very simple program. There are number program where you have to tests two string by ignoring whether the strings are uppercase or lowercase. When you want to test such type of strings for equality in this case-insensitive manner you have to use the equalsIgnoreCase method of the Java String class to achieve result.
String st1 = "monkey";
String st2 = "MONKEY";
if (st1.equalsIgnoreCase(st2))
{
System.out.println("Two strings are the same.")
}
else
{
System.out.println("Two strings are not the same.")
}