最优化的方式来解析在C#中的字符串中的查询字符串 [英] Most optimal way to parse querystring within a string in C#
本文介绍了最优化的方式来解析在C#中的字符串中的查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个普通的字符串中设置查询字符串都值。我开始分裂字符串获得价值了,但我开始怀疑,我可以proabably在一行写这个代替。能否请您指点,如果有这样做更优化的方式?
我想读123和的Request.QueryString但是从正常的字符串abc像
保护无效的Page_Load(对象发件人,EventArgs五)
{
字符串QS =ID = 123安培; XX = ABC;
的String [] = urlInfo qs.Split('和;');
字符串ID = urlInfo [urlInfo.Length - 2];
的Response.Write(id.ToString());
}
解决方案
可以做这种方式:
使用System.Collections.Specialized;
的NameValueCollection查询= HttpUtility.ParseQueryString(的queryString);
的Response.Write(查询[ID]);
希望它帮助。
I have a querystring alike value set in a plain string. I started to split string to get value out but I started to wonder that I can proabably write this in one line instead. Could you please advice if there is more optimal way to do this?
I am trying to read "123" and "abc" like in Request.QueryString but from normal string.
protected void Page_Load(object sender, EventArgs e)
{
string qs = "id=123&xx=abc";
string[] urlInfo = qs.Split('&');
string id = urlInfo[urlInfo.Length - 2];
Response.Write(id.ToString());
}
解决方案
You can do it this way:
using System.Collections.Specialized;
NameValueCollection query = HttpUtility.ParseQueryString(queryString);
Response.Write(query["id"]);
Hope it helps.
这篇关于最优化的方式来解析在C#中的字符串中的查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文