Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 485
Ian Lundberg Avatar asked Mar 28 '12 14:03

Ian Lundberg


People also ask

How do I know if a URL has a specific word?

Use indexOf() to Check if URL Contains a String When a URL contains a string, you can check for the string's existence using the indexOf method from String. prototype. indexOf() . Therefore, the argument of indexOf should be your search string.

How do I know if a URL has a parameter?

To check if a url has query parameters, call the includes() method on the string, passing it a question mark as a parameter, e.g. str. includes('? ') . If the url contains query parameters, the includes method will return true , otherwise false is returned.

How do you check if the URL contains a given string in jQuery?

How do you check if the URL contains a given string in jQuery? Whether you are using jQuery or JavaScript for your frontend, you can simply use the indexOf() method with the href property using windows. The method indexOf() returns the position (a number) of the first occurrence of a given string.

What does URL indexOf do?

The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found.


2 Answers

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")) {
}
like image 184
Brad Christie Avatar answered Oct 01 '22 21:10

Brad Christie


Just use string.contains

if(textBox1.Text.ToLower().Contains(str.ToLower()))
...
like image 30
Justin Pihony Avatar answered Oct 01 '22 20:10

Justin Pihony