목록에서 대소 문자 구분을 무시하는 방법
이 코드가 있다고합시다
string seachKeyword = "";
List<string> sl = new List<string>();
sl.Add("store");
sl.Add("State");
sl.Add("STAMP");
sl.Add("Crawl");
sl.Add("Crow");
List<string> searchResults = sl.FindAll(s => s.Contains(seachKeyword));
포함 검색에서 대소 문자를 무시하려면 어떻게해야합니까?
감사,
가장 좋은 옵션은 대소 문자를 구분하지 않는 서수 비교를 사용하는 것이지만 Contains
방법은이를 지원하지 않습니다.
이를 위해 다음을 사용할 수 있습니다.
sl.FindAll(s => s.IndexOf(searchKeyword, StringComparison.OrdinalIgnoreCase) >= 0);
다음과 같은 확장 메서드로 래핑하는 것이 좋습니다.
public static bool Contains(this string target, string value, StringComparison comparison)
{
return target.IndexOf(value, comparison) >= 0;
}
따라서 다음을 사용할 수 있습니다.
sl.FindAll(s => s.Contains(searchKeyword, StringComparison.OrdinalIgnoreCase));
Linq를 사용하면 .Compare에 새로운 방법이 추가됩니다.
using System.Linq;
using System.Collections.Generic;
List<string> MyList = new List<string>();
MyList.Add(...)
if (MyList.Contains(TestString, StringComparer.CurrentCultureIgnoreCase)) {
//found
}
그래서 아마도
using System.Linq;
...
List<string> searchResults = sl.FindAll(s => s.Contains(seachKeyword, StringComparer.CurrentCultureIgnoreCase));
다음 Contains
과 같이 대소 문자를 구분하지 않는 문자열 같음 비교자를 제공하여 사용할 수 있습니다 .
if (myList.Contains(keyword, StringComparer.OrdinalIgnoreCase))
{
Console.WriteLine("Keyword Exists");
}
최적의 솔루션은 비교를 수행 할 때 케이스를 무시하는 것입니다.
List<string> searchResults = sl.FindAll(s => s.IndexOf(seachKeyword, System.StringComparison.OrdinalIgnoreCase) >= 0);
StringComparer.CurrentCultureIgnoreCase is a better approach instead of using indexOf.
아래 방법은 필요한 키워드를 검색하고 검색된 모든 항목을 새 목록에 삽입 한 다음 새 목록을 반환합니다.
private List<string> serchForElement(string searchText, list<string> ListOfitems)
{
searchText = searchText.ToLower();
List<string> Newlist = (from items in ListOfitems
where items.ToLower().Contains(searchText.ToLower())
select items).ToList<string>();
return Newlist; }
You can apply little trick over this.
Change all the string to same case: either upper or lower case
List searchResults = sl.FindAll(s => s.ToUpper().Contains(seachKeyword.ToUpper()));
For those of you having problems with searching through a LIST of LISTS, I found a solution.
In this example I am searching though a Jagged List and grabbing only the Lists that have the first string matching the argument.
List<List<string>> TEMPList = new List<List<string>>();
TEMPList = JaggedList.FindAll(str => str[0].ToLower().Contains(arg.ToLower()));
DoSomething(TEMPList);
The FindAll does enumeration of the entire list.
the better approach would be to break after finding the first instance.
bool found = list.FirstOrDefault(x=>String.Equals(x, searchKeyWord, Stringcomparison.OrdinalIgnoreCase) != null;
Simply, you can use LINQ query as like below,
String str = "StackOverflow";
int IsExist = Mylist.Where( a => a.item.toLower() == str.toLower()).Count()
if(IsExist > 0)
{
//Found
}
One of possible (may not be the best), is you lowercase all of the strings put into sl. Then you lowercase the searchKeyword.
Another solution is writing another method that lowercase 2 string parameters and compares them
참고URL : https://stackoverflow.com/questions/3107765/how-to-ignore-the-case-sensitivity-in-liststring
'Program Tip' 카테고리의 다른 글
xml.LoadData-루트 수준의 데이터가 잘못되었습니다. (0) | 2020.11.19 |
---|---|
Xamarin.Forms-새 페이지를 만들 때 InitializeComponent가 존재하지 않습니다. (0) | 2020.11.19 |
Android에서 프로그래밍 방식으로 알림 표시 줄에서 알림을 제거하는 방법은 무엇입니까? (0) | 2020.11.19 |
C #을 사용하여 텍스트 파일의 내용 지우기 (0) | 2020.11.19 |
장고에서 관리자 CSS 재정의 (0) | 2020.11.19 |