【问题标题】:C# List of splitted stringsC# 拆分字符串列表
【发布时间】:2020-04-14 20:11:58
【问题描述】:

我有以下问题。 我有这些字符串,它们之间有空格。

"+name:string"            "+age:int"

我用这段代码分割它们:

List<string> stringValueList = new List<string>();
stringValueList = System.Text.RegularExpressions.Regex.Split(stringValue, @"\s{2,}").ToList<string>();

现在 List 的元素是这样的

"+name:string"
"+age:int"

现在我想拆分这些字符串并创建对象。 这看起来像这样:

// Storing the created objects in a List of objects
List<myObject> objectList = new List<myObject>();
for(i = 1; i < stringValueList.Count ; i+=2)
{
   myObject object = new myObject();

   object.modifier = '+';
   object.name = stringValueList[i-1].Trim('+');   // out of the example the object.name should be "name"
   object.type = stringValueList[i];   // out of the example the object.type value should "string"

   objectList.Add(object);
}

最后我应该得到两个具有这些值的对象:

List<myObject> objectList{ myObject object1{modifier = '+' , name ="name" , type="string"}, myObject object2{modifier='+', name="age" type="int"}}

但我的结果是这样的:

List<myObject> objectList {myObject object1 {modifier='+', name="name:string" type="+age:int"}}

所以我得到 1 个对象,而不是 2 个对象。它将两个字符串都放入第一个对象的元素中。

谁能帮帮我?我想我的问题出在 for 循环中,因为 i-1 值是列表中的第一个字符串,而 i 是第二个字符串,但我无法更改。

【问题讨论】:

    标签: c# string list for-loop split


    【解决方案1】:

    我想我的问题出在 for 循环中,因为 i-1 值是 List 中的第一个字符串,而 i 是第二个字符串,但我无法更改。

    我不知道您为什么要这样做i += 2,因为显然您想再次将每个字符串分成两部分。所以必须来改变它。

    使用foreach(),并在循环中再次拆分字符串:

    foreach (var stringValue in stringValueList)
    {
        myObject object = new myObject();
    
        var kvp = stringValue.Split(':');
    
        object.modifier = '+';
        object.name = kvp[0].Trim('+');
        object.type = kvp[1];
    
        objectList.Add(object);
    }
    

    当然,此代码假定您的输入始终有效;您必须添加一些边界检查以使其更加健壮。

    【讨论】:

    • 非常感谢!这正是我所需要的。
    【解决方案2】:

    或者,您可以扩展您的 Regex 公式以一次性完成所有操作。

    例如,使用(?<=")[+](.*?):(.*?)(?="),您所要做的就是分配匹配的组值。

    foreach (Match m in Regex.Matches(stringValue, "(?<=\")[+](.*?):(.*?)(?=\")"))
    {
        myObject obj = new myObject
        {
            modifier = '+',
            name = m.Groups[1].Value,
            type = m.Groups[2].Value
        };
        objectList.Add(obj);
    }
    

    【讨论】:

      【解决方案3】:

      看看其他人如何解决问题很有趣。我会做这样的事情:

      public class MyObject
      {
          public char Modifier { get; set; }
          public string Name { get; set; }
          public string Type { get; set; }
      
          public static IEnumerable<MyObject> Parse(string str)
          {
              return str
              .Split(' ')
              .Where(s => string.IsNullOrEmpty(s) == false)
              .ToList()
              .ForEach(i =>
              {
                  var sections = i.Remove(0, 1).Split(':');
                  return new MyObject()
                  {
                      Modifier = i[0],
                      Name = sections[0],
                      Type = sections[1]
                  };
              });
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-16
        • 1970-01-01
        • 2020-02-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多