C# 字符串。删除不起作用
我有以下问题: 我写了一个程序,它使用谷歌图像搜索来添加到jpg文件的链接。但在链接前面,我有一根15个字符长的绳子,我无法移除C# 字符串。删除不起作用,c#,regex,string,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/im
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);
}
public const int分辨率=1920;
public const int DEFAULTIMGCOUNT=40;
public void getimages(字符串searchpatt)
{
字符串blub=”http://images.google.com/images?q=“+searchpatt+”&biw=“+分辨率;
WebClient客户端=新的WebClient();
string html=client.DownloadString(blub);//下载Google页面;
MatchCollection mc=Regex.Matches(html,
@“(https?:)?/?[^']+?\(jpg | jpeg | gif | png)”;
int mccount=0;//跟踪imgurls
string[]results=新字符串[DEFAULTIMGCOUNT];//放置URL的字符串数组
foreach(mc中的匹配m)//将匹配项放入字符串数组中
{
结果[mccount]=m.值;
mccount++;
}
string remove=“/imgres?imgurl=”;
char[]removetochar=remove.tocharray();
foreach(结果中的字符串s)
{
如果(s!=null)
{
s、 移除(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");
}
}
for(int i=0;i
(我确定这是一个副本,但我无法立即找到。)
NET中的字符串是不可变的。像string.Remove
、string.Replace
等方法不会更改现有字符串的内容,而是返回一个新字符串
所以你想要的是:
s = s.Remove(0, 15);
或者,只需使用子字符串
:
s = s.Substring(15);
由于您知道要转储15个字符,所以可以使用子字符串。请注意,您的大多数代码实际上与您的问题无关,并且您在其中包含了您甚至没有使用的变量。(你为什么打电话给查拉瑞?)我不确定我的失败是否会发生在比赛中。谢谢:)我不知道