How to remove PROTOCOL from URI
You can use this the System.Uri
class like this:
System.Uri uri = new Uri("http://stackoverflow.com/search?q=something");
string uriWithoutScheme = uri.Host + uri.PathAndQuery + uri.Fragment;
This will give you stackoverflow.com/search?q=something
Edit: this also works for about:blank :-)
The best (and to me most beautiful) way is to use the Uri
class for parsing the string to an absolute URI and then use the GetComponents
method with the correct UriComponents
enumeration to remove the scheme:
Uri uri;
if (Uri.TryCreate("http://stackoverflow.com/...", UriKind.Absolute, out uri))
{
return uri.GetComponents(UriComponents.AbsoluteUri &~ UriComponents.Scheme, UriFormat.UriEscaped);
}
For further reference: the UriComponents
enumeration is a decorated with the FlagsAttribute
, so bitwise operations (eg. &
and |
) can be used on it. In this case the &~
removes the bits for UriComponents.Scheme
from UriComponents.AbsoluteUri
using the AND operator in combination with the bitwise complement operator.