2013-12-17 61 views
1

我想將字符串拆分爲2個數組,其中一個使用由vbTab分隔的文本(我認爲它是在c#中的\t),另一個字符串使用由vbtab分隔的測試(我認爲這是在c#中的\n)。根據2個分隔符將字符串拆分爲2個數組

通過搜索,我發現這個(StackOverFlow Question: 1254577):

string input = "abc][rfd][5][,][."; 
string[] parts1 = input.Split(new string[] { "][" }, StringSplitOptions.None); 
string[] parts2 = Regex.Split(input, @"\]\["); 

但我的字符串將是這樣的:

aaa\tbbb\tccc\tddd\teee\nAccount\tType\tCurrency\tBalance\t123,456.78\nDate\tDetails\tAmount\n03NOV13\tTransfer\t9,999,999.00-\n02NOV13\t\Cheque\t125.00\nDebit Card Cash\t200.00 

所以在上面的代碼輸入變爲:

string input = "aa\tbbb\tccc\tddd\teee\nAccount\tType\tPersonal Current Account\tCurrency\tGBP\tBalance\t123,456.78\nDate\tDetails\tAmount\n03NOV13\tTransfer\t9,999,999.00-\n02NOV13\t\Cheque\t125.00\nDebit Card Cash\t200.00\n30OCT13\tLoan Repayment\t1,234.56-\n\tType\t30-Day Notice Savings Account\tCurrency\tGBP\tBalance\t983,456.78\nDate\tDetails\tAmount\n03NOV13\tRepaid\t\250\n" 

但如何創建一個字符串數組w第一個換行和另一個數組,持有一切後?

然後第二個將不得不再分成幾個字符串數組,所以我可以寫一個迷你賬戶的詳細信息,然後顯示每個賬戶的交易。

我希望能夠把原來的串併產生像這樣的A5紙: enter image description here

+0

你能提供exepected陣列的項目嗎? –

回答

0

您可以使用LINQ查詢part1

 var cells = from row in input.Split('\n') 
        select row.Split('\t'); 

你可以只使用First()第一行和剩餘行使用Skip()。例如:

 foreach (string s in cells.First()) 
     { 
      Console.WriteLine("First: " + s); 
     } 

或者

 foreach (string[] row in cells.Skip(1)) 
     { 
      Console.WriteLine(String.Join(",", row)); 
     } 
1

下面的代碼應該做你要求什麼。這導致有5項,part2有26項

string input = "aa\tbbb\tccc\tddd\teee\nAccount\tType\tPersonal Current Account\tCurrency\tGBP\tBalance\t123,456.78\nDate\tDetails\tAmount\n03NOV13\tTransfer\t9,999,999.00-\n02NOV13\t\Cheque\t125.00\nDebit Card Cash\t200.00\n30OCT13\tLoan Repayment\t1,234.56-\n\tType\t30-Day Notice Savings Account\tCurrency\tGBP\tBalance\t983,456.78\nDate\tDetails\tAmount\n03NOV13\tRepaid\t\250\n"; 

// Substring starting at 0 and ending where the first newline begins 
string input1 = input.Substring(0, input.IndexOf(@"\n")); 

/* Substring starting where the first newline begins 
    plus the length of the new line to the end */ 
string input2 = input.Substring(input.IndexOf(@"\n") + 2); 

string[] part1 = Regex.Split(input1, @"\\t"); 
string[] part2 = Regex.Split(input2, @"\\t"); 
相關問題