2013-03-26 43 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