0

我正在获取 cookie 值作为参数。我需要拆分 cookie 值(名称,值)。

string Cookie ="secureToken_sdfsdfbsadbfsdhbh = Tdne68JhO5baix2Ey_K0ICV1HAAFvUP5BA==";
string[] cookieData = Cookie.Split('=')

它甚至还采用了最后一个相等的字符。我需要以下输出。

cookie[0]=secureToken_sdfsdfbsadbfsdhbh
cookie[1]= Tdne68JhO5baix2Ey_K0ICV1HAAFvUP5BA==

它应该只考虑第一个相等的字符。之后它不应该溢出字符串

4

3 回答 3

4

利用:

Cookie.Split(new char[] { '=' }, 2)

它会将您的拆分限制为 2 个字符串

于 2013-10-24T12:13:35.623 回答
1
string Cookie ="secureToken_sdfsdfbsadbfsdhbh = Tdne68JhO5baix2Ey_K0ICV1HAAFvUP5BA==";
string[] cookieData = Cookie.Split(new string[] {" = "}, StringSplitOptions.RemoveEmptyEntries);

foreach (var item in cookieData)
{
   Console.WriteLine(item);
}

输出将是;

secureToken_sdfsdfbsadbfsdhbh
Tdne68JhO5baix2Ey_K0ICV1HAAFvUP5BA==

这里一个Demonstration.

编辑:由于您只需要 2 个拆分部分,因此作为wudzik answers中解释的解决方案,限制会更好。

于 2013-10-24T12:14:22.487 回答
0
string str= "secureToken_sdfsdfbsadbfsdhbh = Tdne68JhO5baix2Ey_K0ICV1HAAFvUP5BA==";
string[] separator = { " = " };
string[] strarr = str.Split(separator, StringSplitOptions.None);
foreach (string s in strarr)
{
    Console.WriteLine(s);
}
Console.ReadKey();
于 2013-10-24T12:27:02.050 回答