开发者

Get root url in asp.net regex

开发者 https://www.devze.com 2023-03-19 07:56 出处:网络
I wonder if I can do this code in one regex: String url = \"ftp://url.com.other/asd\"; url = Regex.Replace( url, \"(https|http|ftp|file)://\",\"\");

I wonder if I can do this code in one regex:

    String url = "ftp://url.com.other/asd";
    url = Regex.Replace( url, "(https|http|ftp|file)://","");
    int index = url.IndexOf("/");
    if (index != -1)
    {
        url = url.Remove(index);
    }
    //resul开发者_JAVA百科t should be: url.com.other

Thanks.


You should use the Uri class for this, more specifically the Host property:

Uri uri = new Uri("ftp://url.com.other/asd");
String hostname = uri.Host;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号