Csharp/CSharp Tutorial/String/String Compare

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Compare for equal

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");   
  
  }
}
str1 == str2
str1 != str3

Compare string case sensitively

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."); 
 
  } 
}
one and ONE are equal ignoring case.

Compare string: equal, less than or greater than

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");  
  
  }
}
str1 is less than str3

Compare strings using StringComparison enumeration: InvariantCulture

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."); 
  } 
}
Password invalid.

Compare string with start index and end index

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."); 
 
  } 
}
First part of one and one, too are equal.

Compare string with String.Compare(string str1, strng str2)

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); 
  } 
}
one is less than two

If two string are equal

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."); 
 
  } 
}
one and one are equal.

String comparisons: ignore case

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);
    }
}
True
False
True
False
False
True

String equality

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));
    }
  }

String Interning

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);
    }
}
Str == : True
Ref == : True
Str == : True
Ref == : False

Use "==" to compare two string objects

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");
       
    }
}
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