programing

String.Contains를 대소 문자를 구분하지 않는 방법?

nasanasas 2020. 10. 31. 09:47
반응형

String.Contains를 대소 문자를 구분하지 않는 방법?


이 질문에 이미 답변이 있습니다.

다음 대소 문자를 구분하지 않으려면 어떻게해야합니까?

myString1.Contains("AbC")

이를 위해 고유 한 확장 메서드를 만들 수 있습니다.

public static bool Contains(this string source, string toCheck, StringComparison comp)
  {
    return source != null && toCheck != null && source.IndexOf(toCheck, comp) >= 0;
  }

그리고 전화 :

 mystring.Contains(myStringToCheck, StringComparison.OrdinalIgnoreCase);

당신이 사용할 수있는:

if (myString1.IndexOf("AbC", StringComparison.OrdinalIgnoreCase) >=0) {
    //...
}

이것은 모든 .NET 버전에서 작동합니다.


bool b = list.Contains("Hello", StringComparer.CurrentCultureIgnoreCase);

[편집] 확장 코드 :

public static bool Contains(this string source, string cont
                                                    , StringComparison compare)
{
    return source.IndexOf(cont, compare) >= 0;
}

이것은 작동 할 수 있습니다 :)

참고 URL : https://stackoverflow.com/questions/17563929/how-to-make-string-contains-case-insensitive

반응형