Results 1 to 5 of 5

Thread: How to compare string using java program regardless of their case?

  1. #1
    Join Date
    Nov 2009
    Posts
    33

    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.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    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.");
            }
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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;
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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.");
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    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.")
    }

Similar Threads

  1. Java program to iterate a subset of a string.
    By Nadiaa in forum Software Development
    Replies: 5
    Last Post: 19-03-2012, 01:53 PM
  2. How to compare text string of two different columns in Excel
    By Chini mao in forum Windows Software
    Replies: 3
    Last Post: 06-01-2012, 04:50 PM
  3. Compare character string in SQL
    By Captain Carrot in forum Software Development
    Replies: 5
    Last Post: 14-05-2010, 10:20 AM
  4. How to write string data to file using java program?
    By Linoo in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 09:26 PM
  5. program to reverse a string in java.
    By Deepest BLUE in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 11:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,917,579.14759 seconds with 17 queries