Csharp/CSharp Tutorial/String/String Compare
Содержание
- 1 Compare for equal
- 2 Compare string case sensitively
- 3 Compare string: equal, less than or greater than
- 4 Compare strings using StringComparison enumeration: InvariantCulture
- 5 Compare string with start index and end index
- 6 Compare string with String.Compare(string str1, strng str2)
- 7 If two string are equal
- 8 String comparisons: ignore case
- 9 String equality
- 10 String Interning
- 11 Use "==" to compare two string objects
Compare for equal
<source lang="csharp">using System;
class MainClass {
public static void Main() { string str1 = "ABCDEabcde1234567890"; string str2 = string.Copy(str1); string str3 = "asdf"; // compare strings if(str1 == str2) Console.WriteLine("str1 == str2"); else Console.WriteLine("str1 != str2"); if(str1 == str3) Console.WriteLine("str1 == str3"); else Console.WriteLine("str1 != str3"); }
}</source>
str1 == str2 str1 != str3
Compare string case sensitively
<source lang="csharp">using System;
class MainClass {
public static void Main() { string str1 = "one"; string str2 = "ONE"; if(String.rupare(str1, str2, true) == 0) Console.WriteLine(str1 + " and " + str2 + " are equal ignoring case."); else Console.WriteLine(str1 + " and " + str2 + " are not equal ignoring case."); }
}</source>
one and ONE are equal ignoring case.
Compare string: equal, less than or greater than
<source lang="csharp">using System;
class MainClass {
public static void Main() { string str1 = "ABCDEabcde1234567890"; string str3 = "C# strings are powerful."; int result = str1.rupareTo(str3); if(result == 0) Console.WriteLine("str1 and str3 are equal"); else if(result < 0) Console.WriteLine("str1 is less than str3"); else Console.WriteLine("str1 is greater than str3"); }
}</source>
str1 is less than str3
Compare strings using StringComparison enumeration: InvariantCulture
<source lang="csharp">using System;
class MainClass {
public static void Main() { string pswd = "asdf"; string str = "fda"; // Compare using invariant culture. if(String.rupare(pswd, str, StringComparison.InvariantCulture) == 0) Console.WriteLine("Password accepted."); else Console.WriteLine("Password invalid."); }
}</source>
Password invalid.
Compare string with start index and end index
<source lang="csharp">using System;
class MainClass {
public static void Main() { string str1 = "one"; string str2 = "one, too"; if(String.rupare(str1, 0, str2, 0, 3) == 0) Console.WriteLine("First part of " + str1 + " and " + str2 + " are equal."); else Console.WriteLine("First part of " + str2 + " and " + str2 + " are not equal."); }
}</source>
First part of one and one, too are equal.
Compare string with String.Compare(string str1, strng str2)
<source lang="csharp">using System;
class MainClass {
public static void Main() { string str1 = "one"; string str2 = "two"; int result = String.rupare(str1, str2); if(result < 0) Console.WriteLine(str1 + " is less than " + str2); else if(result > 0) Console.WriteLine(str1 + " is greater than " + str2); else Console.WriteLine(str1 + " equals " + str2); }
}</source>
one is less than two
If two string are equal
<source lang="csharp">using System;
class MainClass {
public static void Main() { string str1 = "one"; string str2 = "one"; if(String.rupare(str1, str2) == 0) Console.WriteLine(str1 + " and " + str2 + " are equal."); else Console.WriteLine(str1 + " and " + str2 + " are not equal."); }
}</source>
one and one are equal.
String comparisons: ignore case
<source lang="csharp">using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; public class MainClass {
public static void Main() { bool b1 = "hello" == "hello"; Console.WriteLine(b1); bool b2 = "hello" == "hi"; Console.WriteLine(b2); bool b3 = "hello".Equals("hello"); Console.WriteLine(b3); bool b4 = "hello".Equals("hi"); Console.WriteLine(b4); bool b5 = "HoWdY".Equals("howdy"); Console.WriteLine(b5); bool b6 = "HoWdY".Equals("howdy", StringComparison.OrdinalIgnoreCase); Console.WriteLine(b6); }
}</source>
True False True False False True
String equality
<source lang="csharp">using System; using System.Collections.Generic; using System.Text;
class Program { static void Main(string[] args) { string s1 = "Hello!"; string s2 = "Yo!"; Console.WriteLine("s1 = {0}", s1); Console.WriteLine("s2 = {0}", s2); Console.WriteLine(); Console.WriteLine("s1 == s2: {0}", s1 == s2); Console.WriteLine("s1 == Hello!: {0}", s1 == "Hello!"); Console.WriteLine("s1 == HELLO!: {0}", s1 == "HELLO!"); Console.WriteLine("s1 == hello!: {0}", s1 == "hello!"); Console.WriteLine("s1.Equals(s2): {0}", s1.Equals(s2)); Console.WriteLine("Yo.Equals(s2): {0}", "Yo!".Equals(s2)); } }</source>
String Interning
<source lang="csharp">using System; class MainClass {
public static void Main() { string s1 = "Hello"; string s2 = "Hello"; string s3 = "Hello".Substring(0, 4) + "o"; Console.WriteLine("Str == : {0}", s1 == s2); Console.WriteLine("Ref == : {0}", (object) s1 == (object) s2); Console.WriteLine("Str == : {0}", s1 == s3); Console.WriteLine("Ref == : {0}", (object) s1 == (object) s3); }
}</source>
Str == : True Ref == : True Str == : True Ref == : False
Use "==" to compare two string objects
<source lang="csharp">using System; class MainClass {
public static void Main() { string test1 = "This is a test string"; string test2, test3; test2 = test1.Insert(15, "AAA "); test3 = test1.ToUpper(); Console.WriteLine("test1: "{0}"", test1); Console.WriteLine("test2: "{0}"", test2); Console.WriteLine("test3: "{0}"", test3); if (test1 == test3) Console.WriteLine("test1 is equal to test3"); else Console.WriteLine("test1 is not equal to test3"); }
}</source>
test1: "This is a test string" test2: "This is a test AAA string" test3: "THIS IS A TEST STRING" test1 is not equal to test3