拆分并连接 C# 字符串 [英] Split and join C# string
问题描述
可能的重复:
先拆分然后加入字符串的子集
我试图将一个字符串拆分成一个数组,取出第一个元素(使用它),然后将数组的其余部分连接成一个单独的字符串.
I'm trying to split a string into an array, take first element out (use it) and then join the rest of the array into a seperate string.
示例:
theString = "Some Very Large String Here"
会变成:
theArray = [ "Some", "Very", "Large", "String", "Here" ]
然后我想设置变量中的第一个元素并在以后使用它.
Then I want to set the first element in a variable and use it later on.
然后我想将数组的其余部分连接成一个新字符串.
Then I want to join the rest of the array into a new string.
所以它会变成:
firstElem = "Some";
restOfArray = "Very Large String Here"
我知道我可以使用 theArray[0]
作为第一个元素,但是我如何将数组的其余部分连接到一个新字符串?
I know I can use theArray[0]
for the first element, but how would I concatinate the rest of the array to a new string?
推荐答案
你可以使用 string.Split
和 string.Join
:
You can use string.Split
and string.Join
:
string theString = "Some Very Large String Here";
var array = theString.Split(' ');
string firstElem = array.First();
string restOfArray = string.Join(" ", array.Skip(1));
如果你知道你总是只想拆分第一个元素,你可以使用:
If you know you always only want to split off the first element, you can use:
var array = theString.Split(' ', 2);
这样您就不必加入:
string restOfArray = array[1];
这篇关于拆分并连接 C# 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!