string.Remove不起作用

时间:2013-02-24 08:08:05

标签: c# regex string

我有以下问题:

我编写了一个程序,它使用谷歌图像搜索来追加到jpg文件的链接。但是在链接前面我有一个15个字符长的字符串,我无法删除。

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


    }

我尝试删除并修剪,但它们都没有工作,我无法弄清楚我的失败。

我像

一样解决了它
        for (int i = 0; i < results.Count(); i++)
        {
            if (results[i] != null)
            {
                results[i] = results[i].Substring(15);
                Console.Write(results[i]+"\n");
            }
        }

1 个答案:

答案 0 :(得分:26)

(我确定这是重复的,但我不能马上找到一个。)

.NET中的字符串是不可变的。像string.Removestring.Replace等方法不会更改现有字符串的内容 - 它们会返回 new 字符串。

所以你想要这样的东西:

s = s.Remove(0, 15);

或者,只需使用Substring

s = s.Substring(15);
相关问题