How can I split and trim a string into parts all on one line?

前端 未结 8 1097
一个人的身影
一个人的身影 2020-12-22 21:08

I want to split this line:

string line = \"First Name ; string ; firstName\";

into an array of their trimmed versions:

\"Fi         


        
相关标签:
8条回答
  • 2020-12-22 21:26

    Because p.Trim() returns a new string.

    You need to use:

    List<string> parts = line.Split(';').Select(p => p.Trim()).ToList();
    
    0 讨论(0)
  • 2020-12-22 21:32

    The ForEach method doesn't return anything, so you can't assign that to a variable.

    Use the Select extension method instead:

    List<string> parts = line.Split(';').Select(p => p.Trim()).ToList();
    
    0 讨论(0)
提交回复
热议问题