问题描述
||
好的,这个问题可能可以措辞更好。我有一个字符串。
2008 apple micro pc computer
我希望前两个定界符的字符串被“ 1”分隔,然后将其余的保持在一起。所以它会回来
2008
apple
micro pc computer
这是一个组成的字符串,因此可以是任何东西,但仍然是前2个分割,然后所有剩余的都剩下多少
另一个例子
apple orange this is the rest of my string and its so long
退货
apple
orange
this is the rest of my string and its so long
解决方法
传递第二个参数以指定最大拆分成多少个项目。在您的情况下,您将传递3,因此您需要将前两个部分按空格分开,而将字符串的其余部分放在第三个部分。
string myString = \"2008 apple micro pc computer\";
string[] parts = myString.Split(new char[] { \' \' },3);
,这样做:
string s = \"this is a test for something\";
string[] string_array = s.Split(\' \');
int length = string_array.Length;
string first = string_array[0];
string second = string_array[1];
string rest = \"\";
for (int i = 2; i < length; i++) rest = rest + string_array[i] + \" \";
rest.TrimEnd();
,在实现第2点之后,即在附加了前两个单词之后,您可以对其余的单词使用“ 7”。
用string.split
拆分并获取前两个
最后附加Point和Point 2的结果