2010-10-19 44 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

相關問題