-2

我在这里看到了很多帖子,但都在讲述如何逐行读取文本文件。但我不想逐行读取,我想逐行读取。

我的文本文件示例如下

Accepted, 2013/02/22, 20:12, ss123, 1234,1234,1234,Failed*Some reason ,20,500
Rejected, 2013/02/22, 20:12, ss123, 1234,1234,1234,Failed*Some reason ,20,500

输出我想要的文本文件:

Accepted, 2013/02/22, 20:12, ss123, 1234,1234,1234,Accepted,20,500

拒绝,2013/02/22, 20:12, ss123, 1234,1234,1234,Some reason ,20,500

在上面的输出示例中,首先我需要检查该行是否包含 Accepted,如果它包含 Accepted 那么我需要用 Accepted 替换第 8 列(失败*某些原因),否则我需要在第 8 列中只写原因(一些原因)为什么它被拒绝......如果你能解决我的问题,请提前感谢......

我在想是否喜欢以下代码..anybuddy plz帮助我..在此处输入代码

try

{

 System.IO.TextReader ReadFile = new StreamReader("c:\\ATMLOG.txt");

 System.IO.TextWriter writeFile = new StreamWriter("c:\\DeatailReport.txt");

string line = ReadFile.ReadLine()

while(line!=null)
{

if(line.contains("Accepted")
{

string[] strArr = line.Split(',');
strArr[8]="Accepted";//i wanted to replace 8th column of comma separated text file line with Accepted 
TextWrite.writeln(line);
}
else

{

switch(strArr[8])
{
case "some reasson" : //which reason will be mached that should be write on 8th column of comma separated text file

case "some reasson":

; 
;
;



}

            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.ToString());
            }
4

2 回答 2

2

你可以得到一个字段数组String.Split()

string[] strArr = myStr.Split(',');
于 2013-06-03T16:50:01.307 回答
2

我什至不会打扰执行拆分。因为看起来一切都是Failed*Some Reason,你只需要Failed*消失,我会执行替换:

line = line.Replace("Failed*", "");

为了提高效率,您仍然可以检查Accepted.

if (!line.Contains("Accepted"))
{
    line = line.Replace("Failed*", "");
}
于 2013-06-03T16:53:12.433 回答