first you just set the number of , into n, like this int n = 3; string test = "A,B,C,D,E,F,G"; string test1 = test.Split(',').Take(n).Aggregate((a, b) => a + "," + b); string test2 = test.Split(',').Skip(n).Aggregate((a, b) => a + "," + b); Console.WriteLine(test1); Console.WriteLine(test2);
给个正则的 int n = 3; string test = "A,BC,B,DE,E,F,G"; Regex reg = new Regex(@"(?<=^(?:[^,]+,){" + (n-1) + @"}[^,]+),"); string[] result = reg.Split(test); foreach(string s in result) { richTextBox2.Text += s + "\n"; }
第一种方法 int n = 3; string test = "A,B,C,D,E,F,G"; string test1 = test.Split(',').Take(n).Aggregate((a, b) => a + "," + b); string test2 = test.Split(',').Skip(n).Aggregate((a, b) => a + "," + b); Console.WriteLine(test1); Console.WriteLine(test2); 第二种方法 string test = "A,B,C,D,E,F,G"; string test1 = test.Substring(0, 5); string test2 = test.Substring(6, 7); 或者:string test="A,B,C,D,E,F,G"; int n = 5; string test1 = test.Substring(0, n); string test2 = test.Substring(n + 1, test.Length-n-1); Response.Write(test1 + "-" + test2);
string test = "A,B,C,D,E,F,G";
string test1 = test.Split(',').Take(n).Aggregate((a, b) => a + "," + b);
string test2 = test.Split(',').Skip(n).Aggregate((a, b) => a + "," + b);
Console.WriteLine(test1);
Console.WriteLine(test2);
另一种办法:
string test = "A,B,C,D,E,F,G";
string test1 = test.Substring(0, 5);
string test2 = test.Substring(6, 7);
或者:string test="A,B,C,D,E,F,G";
int n = 5;
string test1 = test.Substring(0, n);
string test2 = test.Substring(n + 1, test.Length-n-1);
Response.Write(test1 + "-" + test2);
int n = 3;
string test = "A,BC,B,DE,E,F,G";
Regex reg = new Regex(@"(?<=^(?:[^,]+,){" + (n-1) + @"}[^,]+),");
string[] result = reg.Split(test);
foreach(string s in result)
{
richTextBox2.Text += s + "\n";
}
int n = 3;
string test = "A,B,C,D,E,F,G";
string test1 = test.Split(',').Take(n).Aggregate((a, b) => a + "," + b);
string test2 = test.Split(',').Skip(n).Aggregate((a, b) => a + "," + b);
Console.WriteLine(test1);
Console.WriteLine(test2);
第二种方法
string test = "A,B,C,D,E,F,G";
string test1 = test.Substring(0, 5);
string test2 = test.Substring(6, 7);
或者:string test="A,B,C,D,E,F,G";
int n = 5;
string test1 = test.Substring(0, n);
string test2 = test.Substring(n + 1, test.Length-n-1);
Response.Write(test1 + "-" + test2);
int tmp = test.IndexOf(",",3);
string test1 = test.Substring(0, tmp+1);
string test2 = test.Substring(tmp);
Response.Write(test1 + "-" + test2);