string.Remove不起作用

我有以下问题:

香港专业教育学院写了一个程序,使用谷歌图像搜索extrakst链接到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"); } } 

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

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

所以你想要这样的东西:

 s = s.Remove(0, 15); 

或者,只需使用Substring

 s = s.Substring(15);