Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.Remove doesnt work [duplicate]

Tags:

string

c#

regex

I have the following Problem:

ive written a programm which uses google image search to extrakt links to jpg files. But in front of link i have a 15 chars long string which i cant remove.

    public const int resolution = 1920;
    public const int DEFAULTIMGCOUNT = 40;

    public void getimages(string searchpatt)
    {
        string blub = "http://images.google.com/images?q=" + searchpatt + "&biw=" + resolution;
        WebClient client = new WebClient();

        string html = client.DownloadString(blub);                                              //Downloading the gooogle page;
        MatchCollection mc = Regex.Matches(html,
            @"(https?:)?//?[^'<>]+?\.(jpg|jpeg|gif|png)");

        int mccount = 0;                                                                        // Keep track of imgurls 
        string[] results = new string[DEFAULTIMGCOUNT];                                         // String Array to place the Urls 

        foreach (Match m in mc)                                                                 //put matches in string array
        {
            results[mccount] = m.Value;                
            mccount++;
        }

        string remove = "/imgres?imgurl=";
        char[] removetochar = remove.ToCharArray();

        foreach (string s in results)
        {
            if (s != null)
            {
                s.Remove(0, 15);
                Console.WriteLine(s+"\n");
            }
            else { }
        }
       //  Console.Write(html);


    }

i tried remove and trimstart but none of them is working and i cant figure out my failure.

I solved it like

        for (int i = 0; i < results.Count(); i++)
        {
            if (results[i] != null)
            {
                results[i] = results[i].Substring(15);
                Console.Write(results[i]+"\n");
            }
        }
like image 612
Florian Kopremesis Avatar asked Feb 24 '13 08:02

Florian Kopremesis


1 Answers

(I'm sure this is a duplicate, but I can't immediately find one.)

Strings in .NET are immutable. Methods like string.Remove, string.Replace etc don't change the contents of the existing string - they return a new string.

So you want something like:

s = s.Remove(0, 15);

Or alternatively, just use Substring:

s = s.Substring(15);
like image 89
Jon Skeet Avatar answered Oct 28 '22 11:10

Jon Skeet