2019/07/26

C# 문자열 포함 여부 확인하기.


ToUpper() 를 사용하면 불필요한 문자열을 생성하므로 좋은 방법은 아니다.

string text = "This is an apple.";
string apple = "Apple.";

bool contains = text.ToUpper().Contains(apple.ToUpper());

IndexOf(string, StringComparison) 사용을 추천 한다.

bool contains = text.IndexOf(apple, StringComparison.OrdinalIgnoreCase) >= 0;

확장 함수를 사용하면 더 좋을 것 이다.

public static class StringExtensions
{
    public static bool Contains(this string thisString, string value, StringComparison stringComparison)
    {
        return thisString.IndexOf(value, stringComparison) >= 0;
    }
}

bool contains = text.Contains(apple, StringComparison.OrdinalIgnoreCase);

C# 문자열 포함 여부 확인하기.

ToUpper() 를 사용하면 불필요한 문자열을 생성하므로 좋은 방법은 아니다. string text = "This is an apple." ; string apple = "Apple." ; bool ...