Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#将前两个拆分成字符串,然后将其余的放在一起_C# - Fatal编程技术网

C#将前两个拆分成字符串,然后将其余的放在一起

C#将前两个拆分成字符串,然后将其余的放在一起,c#,C#,好吧,这个问题的措辞可能更好。我有一根绳子 2008 apple micro pc computer 我希望前两个分隔符的字符串按'分割,然后将其余的保留在一起。所以它会回来的 2008 apple micro pc computer 这是一个组成的字符串,所以它可以是任何东西,但它仍然是前2个分裂,然后所有的休息,无论多少是所有的休息 另一个例子 apple orange this is the rest of my string and its so long 返回

好吧,这个问题的措辞可能更好。我有一根绳子

2008 apple micro pc computer
我希望前两个分隔符的字符串按
'
分割,然后将其余的保留在一起。所以它会回来的

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);
  • 在实现第2点之后,即在附加前两个单词之后,可以对其余单词使用
    string.LastIIndexof
  • 使用
    字符串拆分它。拆分
    并获取前两个
  • 最后附加点和点2的结果
  • 这可以做到:

    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();
    
    string s=“这是对某事的测试”;
    字符串[]字符串数组=s.Split(“”);
    int length=string_array.length;
    字符串优先=字符串_数组[0];
    字符串秒=字符串_数组[1];
    字符串rest=“”;
    对于(inti=2;i