APS.Net 2.0. C# provides the functionality to compare the two string using Compare Function. It has some overloaded functions also that provide additional features to compare the strings in ASP.Net based on Culture and Case sensitive aspects and u can also compare substrings of the provided two strings using the overloaded function of C# string.Compare function. C# string compare function returns the integer value based on the lexical relationship between the two strings. It returns 0 if both strings are equal.
- public static int Compare(string strA, string strB);
- public static int Compare(string strA, string strB, bool ignoreCase);
- public static int Compare(string strA, string strB, StringComparison comparisonType);
- public static int Compare(string strA, string strB, bool ignoreCase, CultureInfo culture);
- public static int Compare(string strA, int indexA, string strB, int indexB, int length);
- public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase);
- public static int Compare(string strA, int indexA, string strB, int indexB, int length, StringComparison comparisonType);
- public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase, CultureInfo culture);
Above overloads of string.Compare function accepts different types of parameter values to compare the two strings, substrings of specific length, case sensitive comparison or culture based comparison. C# string compare function returns 0 if strings are equal. It returns value less than 0 i.e. -1, if first string is less than second one. C# string function returns the value greater than 0 i.e. 1, if the first string is greater than second string value passed to the compare function.
Examples of Asp.Net C# string Compare function
Following are examples based on the overloaded functions of C# string compare:
string strA, strB;
Example 1:
strA = "ASP.Net C# string.Compare function"; strB = "ASP.Net C# string.Compare function";
Response.Write(string.Compare(strA, strB) + "<br />");
Output:
0
Example 2:
Response.Write(string.Compare(strA, strB) + "<br />");
Output:
1
Example 3:
strA = "strA"; strB = "string value 2";
Response.Write(string.Compare(strA, strB) + "<br />");
Output:
-1
Example 4:
Response.Write(string.Compare(strA, strB, true) + "<br />");
Output:
0
Example 5:
strA = "compare strings"; strB = "COMPARE STRINGS";
Response.Write(string.Compare(strA, strB, StringComparison.CurrentCultureIgnoreCase) + "<br />");
Output:
0
Example 6:
strA = "compare strings"; strB = "COMPARE STRINGS"; Response.Write(string.Compare(strA, strB, true, System.Globalization.CultureInfo.CurrentCulture) + "<br />");
Output:
0
Example 7:
strA = "compare strings"; strB = "COMPARE STRINGS";
Response.Write(string.Compare(strA, 8, strB, 8, 7) + "<br />");
Output:
-1
Example 8:
strA = "compare strings"; strB = "COMPARE STRINGS";
Response.Write(string.Compare(strA, 8, strB, 8, 7, true) + "<br />");
Output:
0
Example 9:
strA = "compare strings"; strB = "COMPARE STRINGS";
Response.Write(string.Compare(strA, 8, strB, 8, 7, StringComparison.CurrentCultureIgnoreCase) + "<br />");
Output:
0
Example 10:
strA = "compare strings"; strB = "COMPARE STRINGS";
Response.Write(string.Compare(strA, 8, strB, 8, 7, true, System.Globalization.CultureInfo.CurrentCulture) + "<br />");
Output:
0