How to check if url has a substring
I'm trying to do something that will allow all controls if the SubString in the WebBrowser matches the SubString in the textbox, and if it does, it will include all the controls, but I can't seem to get it to work. This is my current code.
string str = "www.google.com";
int pos = str.IndexOf(".") + 1;
int pos2 = str.LastIndexOf(".") - 4;
string str2 = str.Substring(pos, pos2);
if (webBrowser1.Url.AbsoluteUri.Substring(pos, pos2) == textBox1.Text.Substring(pos, pos2))
{
foreach (Control c in Controls)
{
c.Enabled = true;
}
}
Any help would be appreciated.
+3
Ian Lundberg
source
to share
3 answers
The class Uri
is a wonderful thing.
Uri google = new Uri("http://www.google.com/");
if (webBrowser.Url.Host == google.Host){
}
Or even just:
if (webBrower.Url.Host.ToLower().Contains("google")) {
}
+5
Brad christie
source
to share
Just use string.contains
if(textBox1.Text.ToLower().Contains(str.ToLower()))
...
+3
Justin pihony
source
to share
If you ONLY need to know that the substring exists inside the string, use String.Contains
as Justin Pihoni suggested.
If you need information where , in which it exists, use String.IndexOf()
. If the string doesn't exist at all, this method will return -1.
0
David
source
to share