Retrieve URL parameters from string in .NET
If you have a string representing a URL, you may need to retrieve the value of a specific parameter. For this purpose, .NET provides a direct method using the Uri class:
<code>Uri myUri = new Uri("http://www.example.com?param1=good¶m2=bad");</code>
While you can use myUri.Query to get the query string, you will then need to use regular expressions to extract the parameter values. However, there is a cleaner solution:
<code>string param1 = HttpUtility.ParseQueryString(myUri.Query).Get("param1");</code>
The ParseQueryString method of the System.Web.HttpUtility class returns a NameValueCollection object, and the parameter value can be easily accessed using the Get() method:
<code>string param1 = "good";</code>
This method simplifies the task of extracting URL parameters from a string in .NET without the need for complex regular expression parsing.
The above is the detailed content of How to Easily Extract URL Parameters from a String in .NET?. For more information, please follow other related articles on the PHP Chinese website!