我是这么分解的string S=“A=a&B=b&C=c”;
const string VAL1=A;
const string VAL2=B;
const string VAL3=C;
Object o=new Object();
string[] Str=S.splite(new char[]{'&'});
foreach(var val in Str)
{
string[] value=val.splite(new char[]{'='});
if(value[0]==A)
{
o.a=value[1];
}
else if(value[0]==B)
{
o.b=value[1];
}
else if(value[0]==C)
{
o.b=value[1];
}
}
但是我听我师傅说还有一种是用dictionary来做的,可以省略if else if结构,求高手赐教stringdictionary
const string VAL1=A;
const string VAL2=B;
const string VAL3=C;
Object o=new Object();
string[] Str=S.splite(new char[]{'&'});
foreach(var val in Str)
{
string[] value=val.splite(new char[]{'='});
if(value[0]==A)
{
o.a=value[1];
}
else if(value[0]==B)
{
o.b=value[1];
}
else if(value[0]==C)
{
o.b=value[1];
}
}
但是我听我师傅说还有一种是用dictionary来做的,可以省略if else if结构,求高手赐教stringdictionary
Dictionary<string, string> dictionary = s.Split('&').ToDictionary(x => x.Split('=')[0], y => y.Split('=')[1]);
Response.Write(dictionary["B"]); //输出参数B的值
string s = "A=a&B=b&C=c";
NameValueCollection rs = HttpUtility.ParseQueryString(s);
Response.Write(rs["B"]); //输出参数B的值