0

I have a text file which have list of links like this :

<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>

with my program and on the event of button click i want to edit each line of file like :

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

This is what I am trying, but it dosent seem to work, what am I doing wrong?

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);




        }

I have also tried this :

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);
}

and this :

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);
}

but the output is same as the link from link.txt the method is making no change in output.txt

please help

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
sauk
  • 55
  • 8
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Apr 17 '14 at 19:18
  • 1
    @user2889827, please read the answer to that question. Like the OP of that question, you mistakenly believe that `Replace` mutates state. – Kirk Woll Apr 17 '14 at 19:20
  • @JohnSaunders how many times a day do you post this? I see it on so many questions! – Michael McGriff Apr 17 '14 at 19:22
  • 1
    `string abc = HttpUtility.UrlDecode(line.Substring(51, 171));` is what OP is is probably trying to do. – Habib Apr 17 '14 at 19:25
  • if problem is not solved then comment here, i can provide you the code of solution – Zohaib Aslam Apr 17 '14 at 19:28
  • @MichaelMcGriff: as many times as necessary. Sometimes others edit the titles. – John Saunders Apr 17 '14 at 19:32

3 Answers3

3

The reason line isn't changing is because you are not storing the result of the substring and replace functions.

Should be like this:

string parsedLine= null;

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

//use parsedLine here...
T McKeown
  • 12,971
  • 1
  • 25
  • 32
1

You should be using the HttpServerUtility.UrlDecode() method found here: http://msdn.microsoft.com/en-us/library/6196h3wt(v=vs.110).aspx

jensendp
  • 2,135
  • 15
  • 15
0
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);
    }

Look at the replace method. It returns the modified string, it doesn't modify the string you pass in.

CathalMF
  • 9,705
  • 6
  • 70
  • 106
  • 1
    You should explain what the problem was in the code in the question, what you changed, and how that change fixes the problem. – Servy Apr 17 '14 at 19:24
  • thank you but this is not working, cant assign line like that in foreach loop. – sauk Apr 17 '14 at 19:36