Splitting a String into only 2 parts

前端 未结 5 1110
抹茶落季
抹茶落季 2021-02-07 21:52

I want to take a string from a textbox (txtFrom) and save the first word and save whatever is left in another part. (the whatever is left is everything past the first space)

5条回答
  •  孤街浪徒
    2021-02-07 22:25

    You simply combine a split with a join to get the first element:

    string[] items = source.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    
    string firstItem = items[0];
    string remainingItems = string.Join(" ", items.Skip(1).ToList());
    

    You simply take the first item and then reform the remainder back into a string.

提交回复
热议问题