Hello,
I am reading about trim() method right now, wondering, i am not sure maybe the example that i am reading is not explaining it right or is not possible... but... is there a way to trim a string string such as.... for example
thisisahyperlinknexttothishttp://www.gofigure.com/huhokclickonlink
so with this example, i want to remove thisisahyperlinknexttothis, and leave
http://www.gofigure.com/huh and remove okclickonlink, so remove part of the starting and part of the ending and keep only the middle part, is there any way of doing that with trim? note that thisisahyperlink could change to hyperlinkclickitplease to anything so you dont know the length of the first or ending part to get rid of, just want to get ride start position up to http starts and from ending part to the .com part, is there a way?
Thanks
C# trimming string or string manipulation
Started by Siten0308, Jan 04 2010 01:16 PM
3 replies to this topic
#1
Posted 04 January 2010 - 01:16 PM
Its only funny till someone gets hurt.... THEN ITS HILARIOUS :)
|
|
|
#2
Posted 04 January 2010 - 05:36 PM
Do you mean something like this?
If that isn't what you need you should look into Regex as you can do some pretty cool things with it in only a few lines.
string link = "thisisahyperlinknexttothishttp://www.gofigure.com/huhokclickonlink";
int startIndex = link.IndexOf("http://");
int endIndex = link.IndexOf(".com") + 3;
string actualLink = link.Substring(startIndex, endIndex - startIndex);
If that isn't what you need you should look into Regex as you can do some pretty cool things with it in only a few lines.
#3
Posted 05 January 2010 - 02:43 PM
Thank you quack for your help, after careful consideration, i finally decided to go with a different idea, but Quack's idea is also good as well, but after typing it all out and having it to work, i decided that.... that is not me, i would want to simplify my code , by using regex class, now regex as I read is a monster, well i said its a monster because its a whole new can of worms, that i want to open :) and did, after reading some articles, forums and stuff, i got it to work... i think anyways, because it spits out message boxes, but for some reason, it doesnt spit out the websites that match, wondering if anyone can let me know or fill in the answer, that would be great, instead of spits out saying in messagebox: system.blahblah.matchcollection, so i think i got it but how do i now convert int to string variables?
thanks
here is the code below:
thanks
here is the code below:
public void googlewebsitedata()
{
string webdata = webinfo.DownloadString("http://www.google.com/search?hl=en&q="+txt_search.Text);
string pattern = @"^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
for (int i = 0; (i = webdata.IndexOf("class=r><a href=", i)) != -1; i++)
{
MatchCollection matches = rgx.Matches(webdata);
if(matches.Count != -1)
{
MessageBox.Show(matches.ToString());
}
}
}
Its only funny till someone gets hurt.... THEN ITS HILARIOUS :)
#4
Posted 05 January 2010 - 09:37 PM
UPDATE:
i sorta got it to work now with a string instead of matchcollection, which is what i really needed, but for some strange reason, nothing shows up in the message boxes, which there should be atleast 10-14 websitea that should appear in the messageboxes, but nothing, any ideas? here is the code below, again what should happen is it looks at the webdata, then the pattern string it looks at for the websites url, then extracts that using Regex class then puts all of them 1 by 1 in a messagebox which later on will be added into a list, step 1, messagebox, but nothing appears as said, let me know what i did wrong :(
Thanks
code below:
i sorta got it to work now with a string instead of matchcollection, which is what i really needed, but for some strange reason, nothing shows up in the message boxes, which there should be atleast 10-14 websitea that should appear in the messageboxes, but nothing, any ideas? here is the code below, again what should happen is it looks at the webdata, then the pattern string it looks at for the websites url, then extracts that using Regex class then puts all of them 1 by 1 in a messagebox which later on will be added into a list, step 1, messagebox, but nothing appears as said, let me know what i did wrong :(
Thanks
code below:
public void googlewebsitedata()
{
string webdata = webinfo.DownloadString("http://www.google.com/search?hl=en&q="+txt_search.Text);
string pattern = @"^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$";
for (int i = 0; (i = webdata.IndexOf("class=r><a href=", i)) != -1; i++)
{
string matches = Regex.Match(webdata, pattern, RegexOptions.IgnoreCase)
.Groups["Link"].Value;
if(matches.ToString() != null)
{
MessageBox.Show(matches);
}
}
}
Its only funny till someone gets hurt.... THEN ITS HILARIOUS :)


Sign In
Create Account


Back to top









