STORM STORM -4 years ago 121
C# Question

How to get a part of a string (url)?

I have the following url

http://example.com/pa/TaskDetails.aspx?Proj=A5AF5C0D-648A-4892-A995-CDA8013F2643&Assn=2A992D9C-C511-E611-80E4-005056A13B51


I need to extract the
A5AF5C0D-648A-4892-A995-CDA8013F2643
portion of the url parameter:

Proj=A5AF5C0D-648A-4892-A995-CDA8013F2643


This can be in the middle or at the end of the url. I cannot guarantee the position of it. But i always starts with
Proj=
and end with
&
. The string between this is what i want. How can i grab this within C#?

Answer Source

You may need to create a Uri and pass the value of its Query property to the HttpUtility.ParseQueryString method:

string value = HttpUtility.ParseQueryString(new Uri("http://example.com/pa/TaskDetails.aspx?Proj=A5AF5C0D-648A-4892-A995-CDA8013F2643&Assn=2A992D9C-C511-E611-80E4-005056A13B51").Query)["Proj"];

The method is defined in System.Web.dll by the way so you need to add a reference to this one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download