use the Compare() method to compare strings : String Compare « Data Types « C# / C Sharp






use the Compare() method to compare strings

    
using System;

class MainClass {

    public static void Main() {

        int result;
        result = String.Compare("bbc", "abc");
        Console.WriteLine("String.Compare(\"bbc\", \"abc\") = " + result);
        result = String.Compare("abc", "bbc");
        Console.WriteLine("String.Compare(\"abc\", \"bbc\") = " + result);
        result = String.Compare("bbc", "bbc");
        Console.WriteLine("String.Compare(\"bbc\", \"bbc\") = " + result);
        result = String.Compare("bbc", "BBC", true);
        Console.WriteLine("String.Compare(\"bbc\", \"BBC\", true) = " + result);
        result = String.Compare("bbc", "BBC", false);
        Console.WriteLine("String.Compare(\"bbc\", \"BBC\", false) = " + result);
        result = String.Compare("Hello World", 6, "Goodbye World", 8, 5);
        Console.WriteLine("String.Compare(\"Hello World\", 6, " + "\"Goodbye World\", 8, 5) = " + result);
    
    }
}

   
    
    
  








Related examples in the same category

1.Test for equality between stings.
2.use the Equals() method and equality operator to check if two strings are equal
3.Comparing a String to the Beginning or End of a Second StringComparing a String to the Beginning or End of a Second String
4.Compare stringsCompare strings
5.String copy and equalString copy and equal
6.Use String.Compare to check if a URI is a file URI
7.Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean), ignore case
8.Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean), Honor case
9.Check if a String contain another string by ignoring case
10.Compare two strings with StringComparison.CurrentCultureIgnoreCase
11.Checks if the string starts with special character. This does not include any special character from AllowedSpecialCharacters