2010-10-19 50 views
0

我正在使用下面的代码文件名列表的:最好的办法让只有某些集团一个字符串

 //Set up Datatable 
     dtUpgradeFileInfo.Columns.Add("BaseFW"); 
     dtUpgradeFileInfo.Columns.Add("ActiveFW"); 
     dtUpgradeFileInfo.Columns.Add("UpgradeFW"); 
     dtUpgradeFileInfo.Columns.Add("FileName"); 

     //Gets Upgrade information and upgrade Files from Upgrade Folder 
     DirectoryInfo di = new DirectoryInfo(g_strAppPath + "\\Update Files"); 
     FileInfo[] rgFiles = di.GetFiles("*.txt"); 
     foreach (FileInfo fi in rgFiles) 
     { 
      test1 = fi.Name.ToString(); 


     } 

所有文件名会在形式BXXXX_AXXXX_UXXXX。当然,X代表0-9的数字,我需要这3组数字来将每个数据放到数据表的相应列中。我最初打算获得代表每个分组的字符,并将它们组合在一起,但是我想知道是否有比将它发送给charArray更好的方法/更快的方法。有什么建议么?

回答

2

这里是一个相对简单的方式来获得号码的开出TEST1(不含LINQ):

... 
string test1 = fi.Name.ToString(); 

int baseFW=0; 
int activeFW=0; 
int upgradeFW=0; 

// Break the file name into the three groups 
string[] groups=test1.Split('_'); 

if (groups.Length==3) 
{ 
    // Create a numbers array to hold the numbers 
    int[] nums=new int[groups.Length]; 

    // Parse the numbers out of the strings 
    int idx=0; 
    foreach (string s in groups) 
    nums[idx++]=int.Parse(s.Remove(0,1)); // Convert to num 

    baseFW=nums[0]; 
    activeFW=nums[1]; 
    upgradeFW=nums[2]; 
} 
else 
{ 
    // Error handling... 
} 

如果你想做到这一点使用LINQ,那就更简单了:

... 
string test1 = fi.Name.ToString(); 

int baseFW=0; 
int activeFW=0; 
int upgradeFW=0; 

// Extract all numbers 
int[] nums=test1.Split('_') // Split on underscores 
       .Select(s => int.Parse(s.Remove(0,1))) // Convert to ints 
       .ToArray(); // For random access, below 

if (nums.Length==3) 
{ 
    baseFW=nums[0]; 
    activeFW=nums[1]; 
    upgradeFW=nums[2]; 
} 
else 
{ 
    // Error handling... 
} 
2

使用正则表达式允许您轻松解析出所需的值,并且还具有允许跳过目录中不匹配预期文件名格式的文件的额外好处。

您的代码将是这个样子:

 //Gets Upgrade information and upgrade Files from Upgrade Folder 
     string strRegex = @"^B(?<Base>[0-9]{4})_A(?<Active>[0-9]{4})_U(?<Upgrade>[0-9]{4}).txt$"; 
     RegexOptions myRegexOptions = RegexOptions.ExplicitCapture | RegexOptions.Compiled; 
     Regex myRegex = new Regex(strRegex, myRegexOptions); 

     DirectoryInfo di = new DirectoryInfo(g_strAppPath + "\\Update Files"); 
     FileInfo[] rgFiles = di.GetFiles("*.txt"); 
     foreach (FileInfo fi in rgFiles) 
     { 
      string name = fi.Name.ToString(); 
      Match matched = myRegex.Match(name); 
      if (matched.Success) 
      { 
       //do the inserts into the data table here 
       string baseFw = matched.Groups["Base"].Value; 
       string activeFw = matched.Groups["Active"].Value; 
       string upgradeFw = matched.Groups["Upgrade"].Value; 
      } 
     } 
+1

正则表达式是definitly做到这一点的好办法,并留下了花费在解决许多更复杂的问题毫不费力的开口。唯一的耻辱是MS使用Regex比其他解决方案稍微复杂一些。 – Neowizard 2010-10-19 09:28:47

+0

如果输入是固定形式,则效率也不高。性能明智,String.Split应优先使用reg-ex。 – 2010-10-19 14:04:18

+0

@Michael Goldshteyn,这取决于你的优化。从CPU循环的角度来看,精心制作的手写代码解析例程通常能够胜过正则表达式。但是,除非您要处理数千个项目,否则性能差异可能无关紧要。 RegEx方法具有验证文件名与预期模式的一致性的附加好处(某人在目录中丢弃readme_before_running.txt将导致其他方法出错)。另外它可以更容易地调整以支持其他有效格式。 – 2010-10-20 04:32:49

相关问题