CODEKILLER

반응형

String.Equals, String.Compare
String.Equals, String.Compare

public class Example
{
    public static void Main()
    {
        string txt1 = @"codekiller";
        string txt2 = @"Codekiller";

        bool result = txt1.Equals(txt2);
        Console.WriteLine($"Equals 을 이용한 비교: <{txt1}> 와 <{txt2}> 는 {(result ? "동일하다." : "동일하지 않다.")}");

        result = txt1.Equals(txt2, StringComparison.Ordinal);
        Console.WriteLine($"StringComparison.Ordinal 옵셥을 이용한 비교 : <{txt1}> 와 <{txt2}> 는 {(result ? "동일하다." : "동일하지 않다.")}");

        Console.WriteLine(Environment.NewLine);

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        bool result2 = txt1.Equals(txt2, StringComparison.OrdinalIgnoreCase);
        bool areEqual = String.Equals(txt1, txt2, StringComparison.OrdinalIgnoreCase);
        int comparison = String.Compare(txt1, txt2, comparisonType: StringComparison.OrdinalIgnoreCase);

        Console.WriteLine($"Ordinal ignore case: <{txt1}> 와 <{txt2}> 는 {(result2 ? "동일하다." : "동일하지 않다.")}");
        
        if (comparison < 0)
            Console.WriteLine($"<{txt1}> is less than <{txt2}>");
        else if (comparison > 0)
            Console.WriteLine($"<{txt1}> is greater than <{txt2}>");
        else
            Console.WriteLine($"<{txt1}> and <{txt2}> are equivalent in order");
    }
}
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band