Algorithm


  1. Input:

    • Take two strings as input, let's call them str1 and str2.
  2. Length Check:

    • Compare the lengths of str1 and str2. If the lengths are different, the strings are not equal.
  3. Character Comparison:

    • Iterate through each character of both strings using a loop.
    • Compare the characters at the corresponding positions in the strings.
  4. Equality Check:

    • If at any point, you find characters that are not equal, the strings are not equal. Return false.
    • If the loop completes without finding any unequal characters, the strings are equal. Return true.
  5. Case Sensitivity:

    • Decide whether the comparison is case-sensitive or case-insensitive based on your requirements.
      • For case-sensitive comparison, use charAt() for individual character comparison.
      • For case-insensitive comparison, convert both strings (or characters) to lowercase or uppercase using toLowerCase() or toUpperCase().

 

Code Examples

#1 Code Example- Java Programing Compare two strings

Code - Java Programming

public class CompareStrings {

    public static void main(String[] args) {

        String style = "Bold";
        String style2 = "Bold";

        if(style == style2)
            System.out.println("Equal");
        else
            System.out.println("Not Equal");
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Equal

#2 Code Example- Java Programing Compare two strings using equals()

Code - Java Programming

public class CompareStrings {

    public static void main(String[] args) {

        String style = new String("Bold");
        String style2 = new String("Bold");

        if(style.equals(style2))
            System.out.println("Equal");
        else
            System.out.println("Not Equal");
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Equal

#3 Code Example- Compare two string objects using == (Doesn't work)

Code - Java Programming

public class CompareStrings {

    public static void main(String[] args) {

        String style = new String("Bold");
        String style2 = new String("Bold");

        if(style == style2)
            System.out.println("Equal");
        else
            System.out.println("Not Equal");
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Not Equal

#4 Code Example- Different ways to compare two strings

Code - Java Programming

public class CompareStrings {

    public static void main(String[] args) {

        String style = new String("Bold");
        String style2 = new String("Bold");

        boolean result = style.equals("Bold"); // true
        System.out.println(result);

        result = style2 == "Bold"; // false
        System.out.println(result);

        result = style == style2; // false
        System.out.println(result);

        result = "Bold" == "Bold"; // true
        System.out.println(result);
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
true
false
false
true
Advertisements

Demonstration


Java Programing Example to Compare Strings-DevsEnv