简体   繁体   中英

How to remove the last part of url?

I have this url: https://uk.soccerway.com/national/italy/serie-a/20172018/regular-season/r42011/?ICID=TN_02_01_02

I want remove ?ICID=TN_02_01_02 , of couse this string is dynamic so I can't use .Replace() method, any idea?

Use Uri try create ( https://msdn.microsoft.com/en-us/library/ms131572(v=vs.110).aspx ) to create a Uri object.

Then with that Uri object take the "Query" property which will contain the entire query string.

And use "AbsolutePath" to get the URL without the query string.

You can use the Uri class to parse the URL. It allows you to get only up to a specific part using the GetLeftPart method:

public static string GetUriWithoutQuery(string url)
{
    var uri = new Uri(url);
    return uri.GetLeftPart(UriPartial.Path);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM