2013-03-26 79 views
1

我有一個製表符分隔的文件,以及一些字符串包含ý特性而需要用\t所取代。此外,該字符串需要總共包含4個選項卡,並在最後加上任何額外的選項卡。例如,字符串:特色與標籤及必須包含4個總標籤

1234ý5678 
1234 
ý1234ý5678 

應該像

1234\t5678\t\t\t 
1234\t\t\t\t 
\t1234\t5678\t\t 

這是我到目前爲止有:

string[] input_file = (string[])(e.Data.GetData(DataFormats.FileDrop)); 
string output_file = @"c:\filename.txt"; 

foreach (string file in input_file) 
{ 
    string[] lines = File.ReadAllLines(file); 

    for (int i = 0; i < lines.Length; i++) 
    { 
     string line = lines[i]; 

     string[] values = line.Split('\t'); 

     //look at each value in values, replace any ý with a tab, and add 
        //tabs at the end of the value so there are 4 total 

     lines[i] = String.Join("\t", values); 

    } 
    File.WriteAllLines(output_file, lines); 
} 

編輯:澄清 - 整條生產線可能是這樣的:

331766*ALL1 16ý7 14561ý8038 14560ý8037 ausername 11:54:05 12 Nov 2007 

我需要看看每個拼成的線串,並更換了\ t任何Y,並添加\ T的末端以每串共有4這裏的結果應該是什麼樣子:

331766*ALL1 16\t7\t\t\t 14561\t8038\t\t\t 14560\t8037\t\t\t ausername 11:54:05 12 Nov 2007 
+0

將最多4個特殊字符有隻有永遠? – 2013-03-26 17:57:07

+0

'1ý2ý3ý4ý5ý678'會發生什麼? – cvsguimaraes 2013-03-26 17:59:15

+0

@AustinSalonen - 是的,最多4人。 – 2013-03-26 18:00:49

回答

1

你要做的就是:

  1. 分割每行使用\ T作爲分隔符字符串

  2. 遍歷字符串。

  3. 對於每個字符串用\ t替換ý。

  4. 現在計數的字符串中\噸的數量,並根據需要添加額外的\噸。

下面是一些代碼:

string[] lines = System.IO.File.ReadAllLines(input_file); 
var result = new List<string>(); 
foreach(var line in lines) 
{ 
    var strings = line.Split('\t'); 
    var newLine = ""; 
    foreach(var s in strings) 
    { 
     var newString = s.Replace('ý','\t'); 
     var count = newString.Count(f=>f=='\t'); 
     if (count<4) 
      for(int i=0; i<4-count; i++) 
       newString += "\t"; 
     newLine += newString + "\t"; 
    } 
    result.Add(newLine); 
} 
File.WriteAllLines(output_file, result); 

這也可能會被優化使用StringBuilder的速度更好,但它是一個良好的開端。

+0

感謝您的回覆:)這看起來像是在編輯整行,而不是構成行的每個字符串。我用一個更好的例子來更新我的原始問題。 – 2013-03-26 18:57:18

1
private static string SplitAndPadded(string line, string joinedWith = "\t", char splitOn = 'ý') 
{ 
    // 4 required splits yields 5 items (1 | 2 | 3 | 4 | 5) 
    // could/should be a parameter; this allowed for the cleaner comment 
    const int requiredItems = 5; 

    // the empty string case 
    var required = Enumerable.Repeat(string.Empty, requiredItems); 

    // keep empty items; 3rd test case 
    var parts = line.Split(new[] { splitOn }); 

    // this will exclude items when parts.Count() > requiredItems 
    return string.Join(joinedWith, parts.Concat(required).Take(requiredItems)); 
} 


//usage 
// .Select(SplitAndPadded) may need to be .Select(line => SplitAndPadded(line)) 
var lines = File.ReadAllLines(file).Select(SplitAndPadded).ToArray(); 
File.WriteAllLines(outputFile, lines); 

// if input and output files are different, you don't need the ToArray (you can stream) 
+0

我想我明白這是做什麼的,但我需要將這一行分成字符串,然後查看每個字符串,看看是否有一個字符串並用\ t替換。我認爲你的代碼正在看整條線? – 2013-03-26 18:49:19

+0

我已經更新了我的原始問題,並提供了一個更好的示例,說明其中一條線的樣子。 – 2013-03-26 18:56:10

1

試試這個:

string[] lines = System.IO.File.ReadAllLines(input_file); 

for (int i = 0; i < lines.Length; i++) 
{ 
    string line = lines[i]; 
    line = line.Replace("ý", "\t"); 
    int n = line.Split(new string[] { "\t" }, StringSplitOptions.None).Count()-1; 
    string[] temp = new string[4 - n ]; 
    temp = temp.Select(input => "\t").ToArray(); 
    line += string.Join(string.Empty, temp); 
    lines[i] = line; 
} 

System.IO.File.WriteAllLines(output_file, lines); 
+0

感謝您的回覆!在'string [] temp = new string [4 - n];'我得到一個'算術運算導致溢出錯誤。我想這是因爲有些字符串可能沒有要替換的字符? – 2013-03-26 18:43:45

+0

我假設每行中沒有超過4個'\ t'。 – 2013-03-26 18:46:42

+0

整行可能有4個以上的\ t,但組成行的每個字符串都會有<= 4。我需要查看行中的每個字符串,並對每個字符串(而不是行)執行此操作。我希望這是有道理的!我用一個更好的例子來更新我的原始問題。 – 2013-03-26 18:51:23