Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string in C# is a URL [duplicate]

This has been asked before here, but the answers are all PHP related. Is there a similar and working solution using C#? Like a specific test class or routine? I want to parse www.google.com or google.com or mywebsite.net etc... with or without prefixes. Thanks

like image 914
Fandango68 Avatar asked Mar 19 '15 05:03

Fandango68


People also ask

How does == work for strings in c?

Because C strings are array of characters. Arrays are simply pointers to the first element in the array, and when you compare two pointers using == it compares the memory address they point to, not the values that they point to.

How do I check if a string contains something?

The includes() method returns true if a string contains a specified string. Otherwise it returns false .

How do I check for strings?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.

How do I check if a letter is in a string?

You can use string. indexOf('a') . If the char a is present in string : it returns the the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.


1 Answers

https://msdn.microsoft.com/en-us/library/system.web.webpages.validator.regex(v=vs.111).aspx

you use the above mentioned class or use the below regex and check Regex matches with your url string

Regex UrlMatch = new Regex(@"(?i)(http(s)?:\/\/)?(\w{2,25}\.)+\w{3}([a-z0-9\-?=$-_.+!*()]+)(?i)", RegexOptions.Singleline);
Regex UrlMatchOnlyHttps = new Regex(@"(?i)(http(s)?:\/\/)(\w{2,25}\.)+\w{3}([a-z0-9\-?=$-_.+!*()]+)(?i)", RegexOptions.Singleline);

you can also use the above regexpattern to validate the url

like image 181
balaji dileep kumar Avatar answered Oct 17 '22 05:10

balaji dileep kumar