2015-11-06 26 views
0

請幫我分割數據。使用c分割存在於不同行中的數據#

林有我的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]); 
} 
+0

我不清楚你的實際文件是什麼樣子的,//是否是定界符?比使用這個你的'分裂'。是','?在使用循環之前,您可以拆分從分割中獲得的每個字符串。 – HimBromBeere

+0

認爲它不是行ID,它是單元ID,所以我們有一個包含5個值的列。 –

回答

0

如果you're只關心在//前的部分,那麼你也可以使用使用String.Substring()另一種方法:

int num; 
if(int.TryParse(text.Substring(0, text.IndexOf("//")), out num) { 
    // do something with num 
}; 

Alternativly使用Split

int num; 
if(int.TryParse(text.Split("//")[0], out num) { 
    // do something with num 
}; 
0
 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]); 
     } 
0

你在找這個?

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));