string split in c#

后端 未结 2 1740
遥遥无期
遥遥无期 2020-12-12 01:17

I have strings like this:

    00123_MassFlow
    0022245_VOlumeFlow
    122_447_Temperature

I have to split these strings with _

2条回答
  •  忘掉有多难
    2020-12-12 02:00

    If your input is in a single string, you can use string.Split('\n') to get it into a collection:

    string input = @"00123_MassFlow
    0022245_VOlumeFlow
    122_447_Temperature";
    
    var items = input.Split('\n');
    

    Otherwise, I'm going to assume your strings are already in a collection called items. From there, you can use LINQ to accomplish this easily:

    List result = (from x in items
                           let y = x.Trim()
                           select y.Substring(y.LastIndexOf('_') + 1)).ToList();
    

    result will contain the strings MassFlow, VOlumeFlow, and Temperature.

提交回复
热议问题