【问题标题】:read file from a textfile and edit each line programmatically and output it in another txt file [duplicate]从文本文件中读取文件并以编程方式编辑每一行并将其输出到另一个txt文件中[重复]
【发布时间】:2014-04-17 19:17:21
【问题描述】:

我有一个包含如下链接列表的文本文件:

<A title=Accept href="https://www.google.com/url?q=https%3A%2F%2Fregister.eragenx.com%2Freferral%3Freferrer%3Demail%26invitationToken%3D1f7ae07e4cb1ed96a7fc5f6de10376d9%26email%3Dlovessoumi.tra123456789soumit90%2540gmail.com&amp;sa=D&amp;sntz=1&amp;usg=AFQjCNGdIeh6LFs4H5TvysbSV7DjUk2Tuw" target=_blank>Sign Up Now For Free.</A>

使用我的程序并在单击按钮时,我想编辑每一行文件,例如:

https://register.eragenx.com/referral?referrer=email&invitationToken=fd78d9fdf6281e033b389cb14e118f85&email=lovesleeping1234567.8sleepin.g97%40gmail.com

这是我正在尝试的,但它似乎不起作用,我做错了什么?

public void editline()
        {


            string[] lines = File.ReadAllLines("link.txt");

            foreach (var line in lines)
            {
                line.Substring(51,171);
                line.Replace("%3A", ":");
                line.Replace("%2F","/");
                line.Replace("%3F","?");
                line.Replace("%3D" , "=");
                line.Replace("%26","&");

            }

            File.WriteAllLines("output.txt",lines);




        }

我也试过这个:

public void editline()
        {



            string[] lines = File.ReadAllLines("link.txt");



            foreach (var line in lines)
            {

               HttpUtility.UrlDecode(line.Substring(51, 171));


            }


            File.WriteAllLines("output.txt", lines);
}

还有这个:

public void editline()
        {


            string parsedLine = null;
            string[] lines = File.ReadAllLines("link.txt");



            foreach (var line in lines)
            {


                parsedLine = line.Substring(51, 171);
                parsedLine = line.Replace("%3A", ":");
                parsedLine = line.Replace("%2F", "/");
                parsedLine = line.Replace("%3F", "?");
                parsedLine = line.Replace("%3D", "=");
                parsedLine = line.Replace("%26", "&");

            }


            File.WriteAllLines("output.txt", lines);
}

但输出与link.txt中的链接相同,该方法在output.txt中没有改变

请帮忙

【问题讨论】:

  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • @user2889827,请阅读该问题的答案。就像那个问题的 OP 一样,您错误地认为 Replace 会改变状态。
  • @JohnSaunders 你一天发多少次这个?我在很多问题上都看到了!
  • string abc = HttpUtility.UrlDecode(line.Substring(51, 171)); 是 OP 可能正在尝试做的事情。
  • 如果问题没有解决,请在此评论,我可以提供解决方案代码

标签: c# .net string replace


【解决方案1】:

line 没有改变的原因是因为您没有存储substringreplace 函数的结果。

应该是这样的:

string parsedLine= null;

foreach (var line in lines)
{
  parsedLine = line.Substring(51,171);
  parsedLine = parsedLine.Replace("%3A", ":");
  parsedLine = ...
}

//use parsedLine here...

【讨论】:

  • 谢谢,让我试试看:)
  • 我不能在 foreach 循环中这样分配
  • 啊,是的……你需要一个新的变量……我会改变
  • 修复了这个问题,是的,你不能使用line,因为它是只读的,因为它在foreach
  • 嗨,我将文本文件存储到数组行中,然后我使用 foreach 循环遍历每一行,我如何能够将解析后的行存储在 output.txt 文件中。
【解决方案2】:

您应该使用此处的HttpServerUtility.UrlDecode() 方法: http://msdn.microsoft.com/en-us/library/6196h3wt(v=vs.110).aspx

【讨论】:

  • 嗨 jensendp 我需要用替换方法来做。
  • 这是 URL 编码,不是 HTML 编码。
  • 服务,好点。更新了我的答案。
  • 你上面的方法不够灵活,如果引入了新代码,你每次都需要更新代码。
  • @UlugbekUmirov 首先,没有需要解码的编码 HTML 内容。其次,他从 HTML 内容中提取编码的 url。目前他正在使用substring 来做到这一点。因此,虽然他的字符串中有 未编码 html 内容,但他并没有尝试对其进行解码。
【解决方案3】:
public void editline()
    {
        string[] lines = File.ReadAllLines("link.txt");

        foreach (var line in lines)
        {
           line = line.Substring(51,171);
           line = line.Replace("%3A", ":");
          line =  line.Replace("%2F","/");
           line = line.Replace("%3F","?");
           line = line.Replace("%3D" , "=");
           line = line.Replace("%26","&");

        }

        File.WriteAllLines("output.txt",lines);
    }

看看替换方法。它返回修改后的字符串,它不会修改你传入的字符串。

【讨论】:

  • 您应该解释问题中代码的问题所在、您所做的更改以及该更改如何解决问题。
  • 谢谢,但这不起作用,不能在 foreach 循环中分配这样的行。
猜你喜欢
  • 2020-09-07
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
相关资源
最近更新 更多