대소 문자를 구분하지 않는 목록 검색
testList
많은 문자열을 포함 하는 목록 이 있습니다. testList
목록에없는 경우에만 새 문자열을 추가하고 싶습니다 . 따라서 대소 문자를 구분하지 않고 목록을 검색하고 효율적으로 만들어야합니다. Contains
케이스를 고려하지 않기 때문에 사용할 수 없습니다 . 또한 ToUpper/ToLower
성능상의 이유로 사용하고 싶지 않습니다 . 나는이 방법을 발견했다.
if(testList.FindAll(x => x.IndexOf(keyword,
StringComparison.OrdinalIgnoreCase) >= 0).Count > 0)
Console.WriteLine("Found in list");
이것은 작동하지만 부분 단어와도 일치합니다. 목록에 "염소"가 포함되어 있으면 "귀리"가 이미 목록에 있다고 주장하기 때문에 "귀리"를 추가 할 수 없습니다. 단어가 정확히 일치해야하는 대소 문자를 구분하지 않고 목록을 효율적으로 검색하는 방법이 있습니까? 감사
String.IndexOf 대신 String.Equals 를 사용 하여 부분 일치가 없는지 확인하십시오. 또한 모든 요소를 통과하는 FindAll을 사용하지 말고 FindIndex를 사용 하십시오 (첫 번째 요소는 중지합니다).
if(testList.FindIndex(x => x.Equals(keyword,
StringComparison.OrdinalIgnoreCase) ) != -1)
Console.WriteLine("Found in list");
또는 일부 LINQ 방법을 사용하십시오 (첫 번째 방법에서도 중지됨)
if( testList.Any( s => s.Equals(keyword, StringComparison.OrdinalIgnoreCase) ) )
Console.WriteLine("found in list");
나는 이것이 오래된 게시물이라는 것을 알고 있지만 다른 사람이 찾고 있는 경우를 대비하여 대소 문자를 구분하지 않는 문자열 동등 비교기를 제공하여 사용할 수 있습니다 Contains
.
if (testList.Contains(keyword, StringComparer.OrdinalIgnoreCase))
{
Console.WriteLine("Keyword Exists");
}
이것은 msdn 에 따라 .net 2.0부터 사용 가능 합니다.
위의 Adam Sills 답변을 기반으로-여기에 포함 된 멋진 확장 프로그램이 있습니다 ... :)
///----------------------------------------------------------------------
/// <summary>
/// Determines whether the specified list contains the matching string value
/// </summary>
/// <param name="list">The list.</param>
/// <param name="value">The value to match.</param>
/// <param name="ignoreCase">if set to <c>true</c> the case is ignored.</param>
/// <returns>
/// <c>true</c> if the specified list contais the matching string; otherwise, <c>false</c>.
/// </returns>
///----------------------------------------------------------------------
public static bool Contains(this List<string> list, string value, bool ignoreCase = false)
{
return ignoreCase ?
list.Any(s => s.Equals(value, StringComparison.OrdinalIgnoreCase)) :
list.Contains(value);
}
StringComparer를 사용할 수 있습니다 :
var list = new List<string>();
list.Add("cat");
list.Add("dog");
list.Add("moth");
if (list.Contains("MOTH", StringComparer.OrdinalIgnoreCase))
{
Console.WriteLine("found");
}
랜스 라슨 (Lance Larsen) 답변을 기반으로-권장 문자열이있는 확장 방법이 있습니다. 문자열 대신 비교하십시오.
StringComparison 매개 변수를 사용하는 String.Compare의 오버로드를 사용하는 것이 좋습니다. 이러한 오버로드를 통해 의도 한 정확한 비교 동작을 정의 할 수있을뿐만 아니라이를 사용하면 다른 개발자가 코드를보다 쉽게 읽을 수 있습니다. [ 조쉬 프리 @ BCL 팀 블로그 ]
public static bool Contains(this List<string> source, string toCheck, StringComparison comp)
{
return
source != null &&
!string.IsNullOrEmpty(toCheck) &&
source.Any(x => string.Compare(x, toCheck, comp) == 0);
}
IndexOf의 결과가 0보다 크거나 같은지 여부를 확인 중입니다. 즉, 일치가 문자열의 어느 곳 에서 시작되는지를 의미합니다 . 0 과 같은지 확인하십시오 .
if (testList.FindAll(x => x.IndexOf(keyword,
StringComparison.OrdinalIgnoreCase) >= 0).Count > 0)
Console.WriteLine("Found in list");
이제 "염소"와 "귀리"는 일치하지 않지만 "염소"와 "고아"는 일치합니다. 이를 피하기 위해 두 줄의 길이를 비교할 수 있습니다.
To avoid all this complication, you can use a dictionary instead of a list. They key would be the lowercase string, and the value would be the real string. This way, performance isn't hurt because you don't have to use ToLower
for each comparison, but you can still use Contains
.
I had a similar problem, I needed the index of the item but it had to be case insensitive, i looked around the web for a few minutes and found nothing, so I just wrote a small method to get it done, here is what I did:
private static int getCaseInvariantIndex(List<string> ItemsList, string searchItem)
{
List<string> lowercaselist = new List<string>();
foreach (string item in ItemsList)
{
lowercaselist.Add(item.ToLower());
}
return lowercaselist.IndexOf(searchItem.ToLower());
}
Add this code to the same file, and call it like this:
int index = getCaseInvariantIndexFromList(ListOfItems, itemToFind);
Hope this helps, good luck!
참고URL : https://stackoverflow.com/questions/3947126/case-insensitive-list-search
'Programing' 카테고리의 다른 글
오류 발생시 스크립트 종료 (0) | 2020.07.09 |
---|---|
배열에서 속성 값을 합산하는 더 좋은 방법 (0) | 2020.07.09 |
프로그래밍 방식으로 활동의 배경색을 흰색으로 설정하는 방법은 무엇입니까? (0) | 2020.07.09 |
cabal 또는 작업 디렉토리가 프로젝트 디렉토리로 설정된 경우 Emacs Interactive-Haskell이 응답하지 않음 (0) | 2020.07.09 |
같은 이름의 Lambda 캡처 및 매개 변수-다른 사람을 가리는 사람은 누구입니까? (0) | 2020.07.09 |