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");
}
}
(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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With