Split string and get Second value only

前端 未结 5 737
闹比i
闹比i 2021-01-01 02:05

I wonder if it\'s possible to use split to divide a string with several parts that are separated with a comma, like this:

10,12-JUL-16,11,0

5条回答
  •  被撕碎了的回忆
    2021-01-01 02:07

    Yes, you can:

    string[] parts = str.Split(',');
    

    Then your second part is in parts[1].

    or:

    string secondPart = str.Split(',')[1];
    

    or with Linq:

    string secondPart = str.Split(',').Skip(1).FirstOrDefault();
    if (secondPart != null)
    {
        ...
    }
    else
    {
        ... 
    }
    

    Also you can use not only one symbol for string splitting, i.e.:

    string secondPart = str.Split(new[] {',', '.', ';'})[1];
    

提交回复
热议问题