2013-03-06 64 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(); 
     } 
    } 
} 

一些有用的链接开始: