string.Remove不起作用 [英] string.Remove doesnt work

查看:77
本文介绍了string.Remove不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题:

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

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


    }

我尝试了删除和trimstart,但是它们都不起作用,我无法弄清楚我的失败.

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

我像这样解决了

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

推荐答案

(我确定这是重复的,但我无法立即找到它.)

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

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

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

或者,只需使用 Substring :

s = s.Substring(15);

这篇关于string.Remove不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆