fantaghiro
fantaghiro

Reputation: 93

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?

Upvotes: 1

Views: 2909

Answers (2)

ProgrammingLlama
ProgrammingLlama

Reputation: 38850

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);
}

Upvotes: 1

BlythMeister
BlythMeister

Reputation: 299

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.

Upvotes: 2

Related Questions