C# 使用c拆分不同行中的数据#

C# 使用c拆分不同行中的数据#,c#,visual-studio-2013,split,C#,Visual Studio 2013,Split,请帮我拆分数据 我的excel文件中有一些数据 123456 // row id 0,0 234567 // row id 1,0 345678 // row id 2,0 456789 // row id 3,0 等等 现在,我在windows应用程序的文本框中提供这些数据,并希望输出数据为split[0]=123456、split[1]=234567等等 请帮我做这个 string text = textBox1.Text; string[] split = text.Split();//

请帮我拆分数据

我的excel文件中有一些数据

123456 // row id 0,0
234567 // row id 1,0
345678 // row id 2,0
456789 // row id 3,0
等等

现在,我在windows应用程序的文本框中提供这些数据,并希望输出数据为
split[0]=123456、split[1]=234567
等等

请帮我做这个

string text = textBox1.Text;
string[] split = text.Split();//Here what condition should i give???
int count = split.Length;
for (int i = 0; i < count; i++)
{
    Console.WriteLine(split[i]);
}
string text=textBox1.text;
string[]split=text.split()//这里我应该给出什么条件???
int count=split.Length;
for(int i=0;i
如果您只对
/
之前的部分感兴趣,那么您也可以使用另一种方法,使用
String.Substring()

交替使用
拆分

int num;
if(int.TryParse(text.Split("//")[0], out num) {
    // do something with num
};
string text=@”
123456//行id 0,0
234567//行id 1,0
345678//行id 2,0
456789//行id 3,0
";
string[]splits=text.Split('\n')//这里我应该给出什么条件???
string[]split=新字符串[splits.Length];
int j=0;
foreach(拆分中的变量x)
{
拆分[j]=x.拆分(“”)[0];
j++;
}
int count=split.Length;
for(int i=0;i
你在找这个吗

  String text =
    @"123456 // row id 0,0
      234567 // row id 1,0
      345678 // row id 2,0
      456789 // row id 3,0";

  // new Char[] { '\r', '\n' }  - I don't know the actual separator
  // Regex.Match(...) - 1st integer number in the line
  int[] split = text
    .Split(new Char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(line => int.Parse(Regex.Match(line.TrimStart(), "^[0-9]+").Value))
    .ToArray();

  // Test: 
  // 123456
  // 234567
  // 345678
  // 456789
  Console.Write(String.Join(Environment.NewLine, split));

我不清楚您的实际文件是什么样子的,
/
是delimier吗?然后将此项用于您的
拆分
。是不是
?在使用循环之前,您可以拆分从拆分中获得的每个字符串。假设它不是行id,而是单元格id,因此我们有一列5个值。
       string text = @"
        123456 // row id 0,0
          234567 // row id 1,0
         345678 // row id 2,0
           456789 // row id 3,0
        ";
        string[] splits = text.Split('\n');//Here what condition should i give???
      string[] split=new string[splits.Length];
      int j=0;
      foreach (var x in splits)
      {
          split[j]=x.Split(' ')[0];
              j++;
      }
        int count = split.Length;
        for (int i = 0; i < count; i++)
        {
            Console.WriteLine(split[i]);
        }
  String text =
    @"123456 // row id 0,0
      234567 // row id 1,0
      345678 // row id 2,0
      456789 // row id 3,0";

  // new Char[] { '\r', '\n' }  - I don't know the actual separator
  // Regex.Match(...) - 1st integer number in the line
  int[] split = text
    .Split(new Char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(line => int.Parse(Regex.Match(line.TrimStart(), "^[0-9]+").Value))
    .ToArray();

  // Test: 
  // 123456
  // 234567
  // 345678
  // 456789
  Console.Write(String.Join(Environment.NewLine, split));