开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜