我試圖提供一個查詢,如果字串串列與輸入匹配,則僅在大小寫不同時告訴我天氣。請幫忙。
如果輸入是“動物”,那么我需要得到一個真實的。如果輸入是“動物”,那么我應該得到一個錯誤,因為輸入與專案串列中的大小寫完全匹配。我不能說 StringComparison.OrdinalIgnoreCase 因為它總是回傳 true 。
class Program
{
static void Main(string[] args)
{
string abc = "animal";
List<string> items = new List<string>() { "Animal", "Ball" };
if (items.Any(x => x.Matches(abc, StringComparison.Ordinal)))
{
Console.WriteLine("matched");
}
Console.ReadLine();
}
}
static class Extentions
{
public static bool Matches(this string source, string toCheck, StringComparison comp)
{
return source?.IndexOf(toCheck, comp) == 0;
}
}
uj5u.com熱心網友回復:
案例:您可以比較兩次的敏感和區分大小寫:
if (items.Any(item => abc.Equals(item, StringComparison.OrdinalIgnoreCase) &&
abc.Equals(item, StringComparison.Ordinal)))
{
Console.WriteLine("matched");
}
uj5u.com熱心網友回復:
如果我理解正確,我認為您正在尋找這個:
if (items.Any(t => t.ToLower() == abc && t != abc))
{
Console.WriteLine("matched");
}
if 的第一部分將匹配字串以檢查第二部分將確保它們不是相同的情況。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/345368.html
下一篇:C#linq運算子作為變數
