【问题标题】:C# Get substring with specific pattern from stringC# 从字符串中获取具有特定模式的子字符串
【发布时间】:2013-01-30 00:03:38
【问题描述】:

我有一个这样的字符串列表:

List<string> list = new List<string>();
list.Add("Item 1: #item1#");
list.Add("Item 2: #item2#");
list.Add("Item 3: #item3#");

如何获取子字符串 #item1#、#item2# 等并将其添加到新列表中?

如果它包含“#”,我只能通过这样做获得完整的字符串:

foreach (var item in list)
{
    if(item.Contains("#"))
    {
        //Add item to new list
    }
}

【问题讨论】:

  • 具有以下功能的东西:substring(FirstIndexOf('#'), LastIndexOf('#'));
  • 如果字符串不包含#item#部分返回什么?

标签: c# .net string list substring


【解决方案1】:

你可以看看Regex.Match。如果您对正则表达式有所了解(在您的情况下,这将是一个非常简单的模式:"#[^#]+#"),您可以使用它来提取以'#' 开头和结尾的所有项目,以及除@ 之外的任意数量的其他字符987654325@ 介于两者之间。

例子:

Match match = Regex.Match("Item 3: #item3#", "#[^#]+#");
if (match.Success) {
    Console.WriteLine(match.Captures[0].Value); // Will output "#item3#"
}

【讨论】:

    【解决方案2】:

    这个怎么样:

    List<string> substring_list = new List<string>();
    foreach (string item in list)
    {
        int first = item.IndexOf("#");
        int second = item.IndexOf("#", first);
        substring_list.Add(item.Substring(first, second - first);
    }
    

    【讨论】:

      【解决方案3】:

      你可以通过简单地使用:

          List<string> list2 = new List<string>();
          list.ForEach(x => list2.Add(x.Substring(x.IndexOf("#"), x.Length - x.IndexOf("#"))));
      

      【讨论】:

        【解决方案4】:

        试试这个。

        var itemList = new List<string>();
        foreach(var text in list){
        string item = text.Split(':')[1];
        itemList.Add(item);
        
        
        }
        

        【讨论】:

          【解决方案5】:

          LINQ 可以很好地完成这项工作:

          var newList = list.Select(s => '#' + s.Split('#')[1] + '#').ToList();
          

          或者,如果您更喜欢查询表达式:

          var newList = (from s in list
                         select '#' + s.Split('#')[1] + '#').ToList();
          

          或者,您可以使用 Botz3000 建议的正则表达式并将它们与 LINQ 结合:

          var newList = new List(
              from match in list.Select(s => Regex.Match(s, "#[^#]+#"))
              where match.Success
              select match.Captures[0].Value
          );
          

          【讨论】:

            【解决方案6】:

            代码将解决您的问题。 但如果字符串不包含#item#,则将使用原始字符串。

            var inputList = new List<string>
                {
                    "Item 1: #item1#",
                    "Item 2: #item2#",
                    "Item 3: #item3#",
                    "Item 4: item4"
                };
            
            var outputList = inputList
                .Select(item =>
                    {
                        int startPos = item.IndexOf('#');
                        if (startPos < 0)
                            return item;
            
                        int endPos = item.IndexOf('#', startPos + 1);
                        if (endPos < 0)
                            return item;
                        return item.Substring(startPos, endPos - startPos + 1);
                    })
                .ToList();
            

            【讨论】:

              【解决方案7】:

              这是另一种使用 LINQ 正则表达式的方法。 (不确定您的确切要求是否引用了正则表达式,所以现在您可能会遇到两个问题。)

              var list = new List<string> ()
              {
                  "Item 1: #item1#",
                  "Item 2: #item2#",
                  "Item 3: #item3#",
                  "Item 4: #item4#",
                  "Item 5: #item5#",
              };
              
              var pattern = @"#[A-za-z0-9]*#";
              
              list.Select (x => Regex.Match (x, pattern))
                  .Where (x => x.Success)
                  .Select (x => x.Value)
                  .ToList ()
                  .ForEach (Console.WriteLine);
              

              输出:

              #item1#

              #item2#

              #item3#

              #item4#

              #item5#

              【讨论】:

                猜你喜欢
                • 2013-07-18
                • 1970-01-01
                • 2011-10-06
                • 2019-03-20
                • 1970-01-01
                • 2011-05-13
                • 2021-03-10
                • 2012-10-30
                • 2011-05-25
                相关资源
                最近更新 更多