【问题标题】:How to find substring in list of strings using LINQ如何使用 LINQ 在字符串列表中查找子字符串
【发布时间】:2012-03-19 05:01:50
【问题描述】:

我需要使用它的子字符串从字符串集合中找出一个字符串。这个子字符串 必须在开始。

【问题讨论】:

标签: c# linq


【解决方案1】:
collection.FirstOrDefault(s => s.StartsWith(whatever))

【讨论】:

  • 或者,如果可能不止一个,请使用collection.Where(s => s.StartsWith(whatever))
【解决方案2】:

你可以这样做,

List<string> collection = new List<string>();
        collection.Add("example sample");
        collection.Add("sample");
        collection.Add("example");

        var varSubstring = collection.Where(x => x.IndexOf("sample")==0).ToList();
        foreach (var vartemp in varSubstring)
        {
        }

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 2012-05-16
    • 2023-04-03
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多