Sunday, February 12, 2012

4.4.7. Strings comparison

We know how to check if a string has exactly a certain value, using the equality operator (==), but we do not know which string is "greater" than another, and that is necessary if we want to sort texts. The operator "greater than" (>), which we use for numbers, cannot be applied directly to strings. Instead, we use "CompareTo", which will return a number greater than 0 if our string is greater than that indicated as a parameter (or a negative number if our string is shorter, or 0 if equal):

if (sentence.CompareTo("hello") > 0)
       Console.WriteLine("It is greater than hello");

We can also compare ignoring the case (considering uppercase and lowercase equal), if we use String.Compare, which needs as parameters the two strings and a third data, which must be "true" when we want to ignore the case:

if (String.Compare(sentence, "hello", true) > 0)
       Console.WriteLine("It is greater than hello (uppercase or lowercase)");


A complete test program might be:

/*---------------------------*/
/*  C# Example #43c:         */
/*  example43c.cs            */
/*                           */
/*  Text strings (2c)        */
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example43c
{

       public static void Main()
       {
              string sentence;
             
              Console.WriteLine("Enter a word");
              sentence = Console.ReadLine();
             
              // We check if it is exactly “hello”
              if (sentence == "hello")
                    Console.WriteLine("You entered hello");
             
              // Then we check if it is greater
              if (sentence.CompareTo("hello") > 0)
                    Console.WriteLine("It is greater than hello");
              else if (sentence.CompareTo("hello") < 0)
                    Console.WriteLine("It is less than hello");
             
              // And also ignoring the case
              bool ignoreCase = true;
              if (String.Compare(sentence, "hello", ignoreCase) > 0)
                    Console.WriteLine(" It is greater than hello (ignoring case)");
              else if (String.Compare(sentence, "hello", ignoreCase) < 0)
                    Console.WriteLine("It is less than hello (ignoring case)");
              else
                    Console.WriteLine("It is hello (ignoring case)");

       }
}


If we enter a word such as "goal", which starts with a G, which is alphabetically before H, we will be told that the new word is "less than hello":

Enter a word
goal
It is less than hello
It is less than hello (ignoring case)


If we enter "hEllo", which differs from "hello" only in the case, a normal comparison will tell us that is "greater" (uppercase is considered "greater" than lowercase), and a case-insensitive will tell us that they match:

Enter a word
hEllo
It is greater than hello
It is hello (ignoring case)

No comments:

Post a Comment