if (sentence.CompareTo("hello") > 0)
Console.WriteLine("It is greater than hello");
if (String.Compare(sentence, "hello", true) > 0)
Console.WriteLine("It is greater than hello (uppercase or lowercase)");
/*---------------------------*/
/* 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)");
}
}
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