簡體   English   中英

C# 列表<string>包含特定字符串

[英]C# list<string> contains specific string

我有一個字符串列表 (thing1-3, else1-3, other1-3),我想創建一個僅包含 (thing, else, other) 的簡化列表。 看起來很簡單(或者至少這是使用 Classic VB Dictionary .Exists 函數),但我被卡住了。 所以我正在檢查字符串是否以我的簡化字符串之一開頭,然后如果簡化列表不包含該字符串,則添加它。 但是檢查簡化列表是否包含字符串已經讓我失望了。

List<string> myList = new List<string>(new string[] { "thing1", "thing2", "thing3", "else1", "else2", "else3", "other1", "other2", "other3" });

List<string> myListSimplified = new List<string>();

foreach (string s in myList)
{
   if (s.StartsWith("thing"))
   {
      if (!myListSimplifed.Contains("thing")) { myListSimplifed.Add("thing"); }
   }

   if (s.StartsWith("else"))
   {
      if (!myListSimplifed.Contains("else")) { myListSimplifed.Add("else"); }
   }

   if (s.StartsWith("other"))
   {
      if (!myListSimplifed.Contains("other")) { myListSimplifed.Add("other"); }
   }
}

我希望這個 mySimplifiedList 包含“thing”、“else”、“other”,但它包含 thing1-3、else1-2、other1-3。

if (myListSimplified.Exists("thing")) { }

IntelliSense 返回“無法從‘字符串’轉換為‘System.Predicate’

好的..所以這個:

if (!myListSimplified.Any(str => str.Contains("thing"))) { myListSimplified.Add("thing"); }

或者

if (!myListSimplified.Exists(str => str.Contains("thing"))) { myListSimplified.Add("thing"); }

這些都不起作用。

顯然,我可以創建一個方法來遍歷列表並將其與字符串進行比較,但是此功能對於 MS 遺漏的列表來說似乎太基本了......傳遞列表似乎也很愚蠢......

private bool Exists(List<string> lList, string sCompare)
{
    bool bVal = false;

    foreach (string s in lList)
    {
        if (s == sCompare) { bVal = true; }
        break;
    }

    return bVal;
}

我不確定你的問題是什么:

首先,您的第一個代碼片段似乎包含一個錯字:您有List<string> myListSimplified但隨后在foreach您引用了myListSimplifed (在“f”之后缺少“i”)。

如果我更正了那個錯字並運行了你的代碼,那么我會得到一個包含{"thing", "else", "other" } ,這似乎也是你所期望的。

除了 myListSimplifed 與 myListSimplified 中的拼寫錯誤之外,您的代碼示例會生成您想要它執行的操作。

不是您所要求的,但您可以使用更少的代碼行獲得相同的效果:

var myList = new List<string> {"thing1", "thing2", "thing3", "else1", "else2", "else3", "other1", "other2", "other3"};
var myListSimplified = myList.Select(s => new string(s.Where(char.IsLetter).ToArray())).Distinct();

列表是通用數據類型。 它們不知道什么是字符串,因此它們沒有提供現成的工具來搜索StartWith包含其他字符串的項目。 一般來說,這種操作沒有意義。 這就是為什么你有像Any這樣的操作使用 lambda 函數來提供你自己的邏輯:

if (myList.Any(str => str.StartsWith("thing")))
     mySimplifiedList.Add("thing");

我已經測試過了,它工作正常。

當然,如果您有多個要提取的字符串,您可能應該使其更通用。 最簡單的方法是將上面的行提取到接收子字符串(在本例中為“thing”)作為參數的方法中。 更通用的方法(假設它與您的數據和邏輯匹配)是遍歷所有字符串,從中去除所有數字,然后存儲它。 假設StripNumerals是一種接收字符串的方法,它可能看起來像這樣, Distinct確保每個字符串只有一個實例。

var simplifiedList = myList.Select(StripNumerals).Distinct().ToList();

將代碼放入 Visual Studio 時,我注意到您的錯字。 結果是正確的,但您的算法遠非通用。 你可以嘗試什么:

  1. var有助於簡化聲明
  2. 列表初始化可以簡化
  3. 通過剝離所有數字獲得列表並對其執行不同的操作

    var myList = new List<string>() { "thing1", "thing2", "thing3", "else1", "else2", "else3", "other1", "other2", "other3" }; var listWithoutNumbers = myList.Select(s => { Regex rgx = new Regex("[0-9]"); return rgx.Replace(s, ""); }); var simplifiedList = listWithoutNumbers.Distinct();

除了錯別字外,您的原始解決方案有效。 但是,如果你想要更通用的解決方案,你可以使用這樣的東西

List<string> myList = new List<string>(new string[] { "thing1", "thing2", "thing3", "else1", "else2", "else3", "other1", "other2", "other3" });

List<string> myListSimplified = myList.Select(s => new String(s.Where(Char.IsLetter).ToArray())).Distinct().ToList();

不要忘記添加

using System.Linq;

如果你會嘗試這個解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM