w3resource

Scala Programming: Compare a given string to another string, ignoring case considerations

Scala Programming String Exercise-9 with Solution

Write a Scala program to compare a given string to another string, ignoring case considerations.

Sample Solution:

Scala Code:

object Scala_String {
  
  def test(str1: String, str2: String): Boolean = {
    str1.equalsIgnoreCase(str2)   
  }

  def main(args: Array[String]): Unit = {
        val columnist1 ="Stephen Edwin King";
        val columnist2 ="Stephen Edwin  King";
        val columnist3 ="Stephen edwin king";

        // Are any of the above Strings equal to one another?
        val equals1 = test(columnist1,columnist2)
        val equals2 = test(columnist1,columnist3)

        // Display the results of the equality checks.
        System.out.println("\"" + columnist1 + "\" equals \"" +
            columnist2 + "\"? " + equals1);
        System.out.println("\"" + columnist1 + "\" equals \"" +
            columnist3 + "\"? " + equals2);
      }
}

Sample Output:

"Stephen Edwin King" equals "Stephen Edwin  King"? false
"Stephen Edwin King" equals "Stephen edwin king"? true

Scala Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Scala program to check whether two String objects contain the same data.
Next: Write a Scala program to replace a specified character with another character.

What is the difficulty level of this exercise?