本文整理汇总了C#中System.String.Compare方法的典型用法代码示例。如果您正苦于以下问题:C# String.Compare方法的具体用法?C# String.Compare怎么用?C# String.Compare使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.Compare方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
// Create upper-case characters from their Unicode code units.
String stringUpper = "\x0041\x0042\x0043";
// Create lower-case characters from their Unicode code units.
String stringLower = "\x0061\x0062\x0063";
// Display the strings.
Console.WriteLine("Comparing '{0}' and '{1}':",
stringUpper, stringLower);
// Compare the uppercased strings; the result is true.
Console.WriteLine("The Strings are equal when capitalized? {0}",
String.Compare(stringUpper.ToUpper(), stringLower.ToUpper()) == 0
? "true" : "false");
// The previous method call is equivalent to this Compare method, which ignores case.
Console.WriteLine("The Strings are equal when case is ignored? {0}",
String.Compare(stringUpper, stringLower, true) == 0
? "true" : "false" );
输出:
Comparing 'ABC' and 'abc': The Strings are equal when capitalized? true The Strings are equal when case is ignored? true
示例2: Main
//引入命名空间
using System;
using System.Text;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("Eric");
myAL.Add("Mark");
myAL.Add("Lance");
myAL.Add("Rob");
myAL.Add("Kris");
myAL.Add("Brad");
myAL.Add("Kit");
myAL.Add("Bradley");
myAL.Add("Keith");
myAL.Add("Susan");
// Displays the properties and values of the ArrayList.
Console.WriteLine( "Count: {0}", myAL.Count );
PrintValues ("Unsorted", myAL );
myAL.Sort();
PrintValues("Sorted", myAL );
myAL.Sort(new ReverseStringComparer() );
PrintValues ("Reverse" , myAL );
string [] names = (string[]) myAL.ToArray (typeof(string));
}
public static void PrintValues(string title, IEnumerable myList ) {
Console.Write ("{0,10}: ", title);
StringBuilder sb = new StringBuilder();
foreach (string s in myList) {
sb.AppendFormat( "{0}, ", s);
}
sb.Remove (sb.Length-2,2);
Console.WriteLine(sb);
}
}
public class ReverseStringComparer : IComparer {
public int Compare (object x, object y) {
string s1 = x as string;
string s2 = y as string;
//negate the return value to get the reverse order
return - String.Compare (s1,s2);
}
}
示例3: IsFileURI
static bool IsFileURI(String path)
{
return (String.Compare(path, 0, "file:", 0, 5, true) == 0);
}
示例4: IsFileURI
static bool IsFileURI(String path)
{
return (String.Compare(path, 0, "file:", 0, 5, StringComparison.OrdinalIgnoreCase) == 0);
}
示例5: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
string s1 = "ani\u00ADmal";
string s2 = "animal";
Console.WriteLine("Comparison of '{0}' and '{1}': {2}",
s1, s2, String.Compare(s1, s2));
// The example displays the following output:
// Comparison of 'ani-mal' and 'animal': 0
}
}
示例6: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
string s1 = "Ani\u00ADmal";
string s2 = "animal";
Console.WriteLine("Comparison of '{0}' and '{1}': {2}",
s1, s2, String.Compare(s1, s2, true));
// The example displays the following output:
// Comparison of 'Ani-mal' and 'animal': 0
}
}
示例7: Main
// This example demonstrates the
// System.String.Compare(String, String, StringComparison) method.
using System;
using System.Threading;
class Sample
{
public static void Main()
{
string intro = "Compare three versions of the letter I using different " +
"values of StringComparison.";
// Define an array of strings where each element contains a version of the
// letter I. (An array of strings is used so you can easily modify this
// code example to test additional or different combinations of strings.)
string[] threeIs = new string[3];
// LATIN SMALL LETTER I (U+0069)
threeIs[0] = "\u0069";
// LATIN SMALL LETTER DOTLESS I (U+0131)
threeIs[1] = "\u0131";
// LATIN CAPITAL LETTER I (U+0049)
threeIs[2] = "\u0049";
string[] unicodeNames =
{
"LATIN SMALL LETTER I (U+0069)",
"LATIN SMALL LETTER DOTLESS I (U+0131)",
"LATIN CAPITAL LETTER I (U+0049)"
};
StringComparison[] scValues =
{
StringComparison.CurrentCulture,
StringComparison.CurrentCultureIgnoreCase,
StringComparison.InvariantCulture,
StringComparison.InvariantCultureIgnoreCase,
StringComparison.Ordinal,
StringComparison.OrdinalIgnoreCase
};
Console.Clear();
Console.WriteLine(intro);
// Display the current culture because the culture-specific comparisons
// can produce different results with different cultures.
Console.WriteLine(
"The current culture is {0}.\n", Thread.CurrentThread.CurrentCulture.Name);
// Determine the relative sort order of three versions of the letter I.
foreach (StringComparison sc in scValues)
{
Console.WriteLine("StringComparison.{0}:", sc);
// LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I (U+0131)
Test(0, 1, sc, threeIs, unicodeNames);
// LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049)
Test(0, 2, sc, threeIs, unicodeNames);
// LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I (U+0049)
Test(1, 2, sc, threeIs, unicodeNames);
Console.WriteLine();
}
}
protected static void Test(
int x, int y, StringComparison comparison, string[] testI, string[] testNames)
{
string resultFmt = "{0} is {1} {2}";
string result = "equal to";
int cmpValue = 0;
cmpValue = String.Compare(testI[x], testI[y], comparison);
if (cmpValue < 0)
result = "less than";
else if (cmpValue > 0)
result = "greater than";
Console.WriteLine(resultFmt, testNames[x], result, testNames[y]);
}
}
输出:
Compare three versions of the letter I using different values of StringComparison. The current culture is en-US. StringComparison.CurrentCulture: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.CurrentCultureIgnoreCase: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.InvariantCulture: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.InvariantCultureIgnoreCase: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.Ordinal: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) StringComparison.OrdinalIgnoreCase: LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)
示例8: Main
//引入命名空间
using System;
using System.Globalization;
class Sample {
public static void Main() {
String str1 = "change";
String str2 = "dollar";
String relation = null;
relation = symbol( String.Compare(str1, str2, false, new CultureInfo("en-US")) );
Console.WriteLine("For en-US: {0} {1} {2}", str1, relation, str2);
relation = symbol( String.Compare(str1, str2, false, new CultureInfo("cs-CZ")) );
Console.WriteLine("For cs-CZ: {0} {1} {2}", str1, relation, str2);
}
private static String symbol(int r) {
String s = "=";
if (r < 0) s = "<";
else if (r > 0) s = ">";
return s;
}
}
输出:
For en-US: change < dollar For cs-CZ: change > dollar
示例9: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string s1 = "Ani\u00ADmal";
string s2 = "animal";
Console.WriteLine("Comparison of '{0}' and '{1}': {2}",
s1, s2, String.Compare(s1, s2, true,
CultureInfo.InvariantCulture));
// The example displays the following output:
// Comparison of 'Ani-mal' and 'animal': 0
}
}
示例10: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string string1 = "brother";
string string2 = "Brother";
string relation;
int result;
// Cultural (linguistic) comparison.
result = String.Compare(string1, string2, new CultureInfo("en-US"),
CompareOptions.None);
if (result > 0)
relation = "comes after";
else if (result == 0)
relation = "is the same as";
else
relation = "comes before";
Console.WriteLine("'{0}' {1} '{2}'.",
string1, relation, string2);
// Cultural (linguistic) case-insensitive comparison.
result = String.Compare(string1, string2, new CultureInfo("en-US"),
CompareOptions.IgnoreCase);
if (result > 0)
relation = "comes after";
else if (result == 0)
relation = "is the same as";
else
relation = "comes before";
Console.WriteLine("'{0}' {1} '{2}'.",
string1, relation, string2);
// Culture-insensitive ordinal comparison.
result = String.CompareOrdinal(string1, string2);
if (result > 0)
relation = "comes after";
else if (result == 0)
relation = "is the same as";
else
relation = "comes before";
Console.WriteLine("'{0}' {1} '{2}'.",
string1, relation, string2);
// The example produces the following output:
// 'brother' comes before 'Brother'.
// 'brother' is the same as 'Brother'.
// 'brother' comes after 'Brother'.
}
}
示例11:
String str1 = "machine";
String str2 = "device";
String str;
int result;
Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
result = String.Compare(str1, 2, str2, 0, 2);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2);
输出:
str1 = 'machine', str2 = 'device' Substring 'ch' in 'machine' is less than substring 'de' in 'device'.
示例12:
String str1 = "MACHINE";
String str2 = "machine";
String str;
int result;
Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
Console.WriteLine("Ignore case:");
result = String.Compare(str1, 2, str2, 2, 2, true);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2);
Console.WriteLine();
Console.WriteLine("Honor case:");
result = String.Compare(str1, 2, str2, 2, 2, false);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2);
输出:
str1 = 'MACHINE', str2 = 'machine' Ignore case: Substring 'CH' in 'MACHINE' is equal to substring 'ch' in 'machine'. Honor case: Substring 'CH' in 'MACHINE' is greater than substring 'ch' in 'machine'.
示例13: Main
// Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean, CultureInfo)
using System;
using System.Globalization;
class Sample {
public static void Main() {
// 0123456
String str1 = "MACHINE";
String str2 = "machine";
String str;
int result;
Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
Console.WriteLine("Ignore case, Turkish culture:");
result = String.Compare(str1, 4, str2, 4, 2, true, new CultureInfo("tr-TR"));
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2);
Console.WriteLine();
Console.WriteLine("Ignore case, invariant culture:");
result = String.Compare(str1, 4, str2, 4, 2, true, CultureInfo.InvariantCulture);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2);
}
}
输出:
str1 = 'MACHINE', str2 = 'machine' Ignore case, Turkish culture: Substring 'IN' in 'MACHINE' is less than substring 'in' in 'machine'. Ignore case, invariant culture: Substring 'IN' in 'MACHINE' is equal to substring 'in' in 'machine'.
示例14: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string name1 = "Jack Smith";
string name2 = "John Doe";
// Get position of space character.
int index1 = name1.IndexOf(" ");
index1 = index1 < 0 ? 0 : index1--;
int index2 = name2.IndexOf(" ");
index1 = index1 < 0 ? 0 : index1--;
int length = Math.Max(name1.Length, name2.Length);
Console.WriteLine("Sorted alphabetically by last name:");
if (String.Compare(name1, index1, name2, index2, length,
new CultureInfo("en-US"), CompareOptions.IgnoreCase) < 0)
Console.WriteLine("{0}\n{1}", name1, name2);
else
Console.WriteLine("{0}\n{1}", name2, name1);
}
}
输出:
Sorted alphabetically by last name: John Doe Jack Smith
示例15: String.Compare(String value1, String value2)
//引入命名空间
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);
}
}