2013-03-06 72 views
0

井柱,我有這個信息文件:如何分割這種在C#

04/12/2012 2012年6月12日XX123410116000020000118 XEPLATINOXE XX XXXX XXXEXX XX PLATINOX $ 131.07

這是一個完整的線,到該文件中我有10日線更是這樣,我想用分在C#中獲取下一個結果:

Line[0]= 04/12/2012 
Line[1]= 06/12/2012 
Line[2]= XX123410116000020000118 
Line[3]= XEPLATINOXE XX XXXEXX XXXX PLATINOX XX 
Line[4]= $  131.07 

我嘗試這樣做,但不工作, 請幫幫我。

謝謝。

祝福!

+3

它是如何一致的?它總是四個字符串,然後是一個美元金額?你需要美元符號在最後的字符串? – 2013-03-06 14:59:12

回答

0

那麼,有人發佈這個答案,它適用於我!

String[] array1 = file_[count + 19].Split(new[] { " " }, 4,StringSplitOptions.RemoveEmptyEntries); 

我不需要最後一個數組拆分,在這種情況下:

array[3]  

,因爲它爲我好以下格式:

XEPLATINOXE XX XXXX XXXEXX XX PLATINOX $ 131.07

非常感謝!

祝福!

-1

String.Substring Method (Int32, Int32)

這已經撒手人寰。

用法示例:

String myString = "abc"; 
bool test1 = myString.Substring(2, 1).Equals("c"); // This is true. 
Console.WriteLine(test1); 
bool test2 = String.IsNullOrEmpty(myString.Substring(3, 0)); // This is true. 
Console.WriteLine(test2); 
try { 
    string str3 = myString.Substring(3, 1); // This throws ArgumentOutOfRangeException. 
    Console.WriteLine(str3); 
} 
catch (ArgumentOutOfRangeException e) { 
    Console.WriteLine(e.Message); 
} 
+0

只是提到了: 這隻有在字符串的長度和子字符串的長度固定時纔有效。 – jAC 2013-03-06 15:05:03

+0

不可以。您可以從任何字符串獲取字符的索引(空格,'$'等)。如果'(-1 jp2code 2013-03-06 16:27:24

1

我敢肯定有人會提出一個奇特的正則表達式,但這裏有一個辦法做到這一點沒有一個:

string source = "04/12/2012 06/12/2012 XX123410116000020000118 XEPLATINOXE XX XXXEXX XXXX PLATINOX XX $ 131.07"; 
string[] split1 = source.Split('$'); 
string[] split2 = split1[0].Split(new char[] {' '},4); // limit to 4 results 
string lines = split2.Concat(new [] {split1[1]}); 
0

04/12/2012 6月12日/ 2012 XX123410116000020000118 XEPLATINOXE XX XXXEXX XXXX PLATINOX XX $ 131.07

可以通過使用分組大括號用正則表達式解析出來,但爲了得到真正可靠的結果,我們需要知道記錄的哪些部分是一致的。

我要去承擔的第三項從來沒有空格,而第五項總是以$

using System; 
using System.Text.RegularExpressions; 

class Program 
{ 
    static void Main() 
    { 
     // First we see the input string. 
     string input = "04/12/2012 06/12/2012 XX123410116000020000118 XEPLATINOXE XX XXXEXX XXXX PLATINOX XX $ 131.07"; 

     // Here we call Regex.Match. 
     Match match = Regex.Match(input, @"^(\d\d\/\d\d\/\d{4}) (\d\d\/\d\d\/\d{4}) (\S+) ([^\$]+) (\$.+)$"); 

     // Here we check the Match instance. 
     if (match.Success) 
     { 
      // Your results are stored in match.Groups[1].Value, match.Groups[2].Value, match.Groups[3].Value, 
      //  match.Groups[4].Value, and match.Groups[5].Value, so now you can do whatever with them 
      Console.WriteLine(match.Groups[5].ToString()); 
      Console.ReadKey(); 
     } 
    } 
} 

一些有用的鏈接開始: