How to match the first occurrence of a character and split it

后端 未结 5 1292
忘掉有多难
忘掉有多难 2021-01-26 16:24

I have a text file from which I want to store Keys and Values in a String array.

In this case, Key is something like &

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-26 16:33

    Basically, you do not want to split your entire string, but to skip all the content before encountering first ':' char plus one symbol (':' itself).

    var data = line.Substring(line.IndexOf(':') + 1);
    

    Or if you really want solution with Split:

    var data = string.Join(":", line.Split(':').Skip(1));
    

    Here, we first split the string into array, then skip one element (the one we are trying to get rid of), and finally construct a new string with ':' between elements in the array.

提交回复
热议问题