Get argument value from string input
How do 开发者_如何学编程I create a class method to get the argument from a string input?
string value GetArugmentValueByName (string input, string name)
Example
myInput="code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6&
client_id=21302922996.apps.googleusercontent.com&
client_secret=XTHhXh1SlUNgvyWGwDk1EjXB&
redirect_uri=https://www.example.com/back&
grant_type=authorization_code";
If I do this
string myGrantType = GetArugmentValueByName(myInput, "grant_type");
the value of myGrantType should equal "authorization_code"
You can use ParseQueryString to parse out the params into a name value collection, then index into that for the param you are looking for.
public string GetArgumentValueByName(string queryString, string paramName)
{
var paramCol = HttpUtility.ParseQueryString(queryString);
return paramCol[paramName] ?? string.Empty;
}
精彩评论