Ian Lundberg
Ian Lundberg

Reputation: 1883

how to check if a url has a substring

I am trying to make something that will enable all controls if a SubString in a WebBrowser matches a SubString in a TextBox and if it does it will enable all 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 and all help will be appreciated.

Upvotes: 5

Views: 9231

Answers (3)

Justin Pihony
Justin Pihony

Reputation: 67095

Just use string.contains

if(textBox1.Text.ToLower().Contains(str.ToLower()))
...

Upvotes: 3

David
David

Reputation: 73574

If you ONLY need to know that the substring exists within the string, use String.Contains as Justin Pihony suggested.

If you need to know where in the string it exists, use String.IndexOf(). If the string doesn't exist at all, this method will return -1.

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101614

The Uri class is a wonderful thing.

Uri google = new Uri("http://www.google.com/");
if (webBrowser.Url.Host == google.Host){
}

Or even simply:

if (webBrower.Url.Host.ToLower().Contains("google")) {
}

Upvotes: 8

Related Questions