如何分割以下字符串?

时间:2013-10-24 12:11:04

标签: c# c#-4.0

我将cookie值作为参数。我需要拆分cookie值(名称,值)。

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

它甚至也取得了最后一个相同的字符。我需要跟随输出。

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

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

3 个答案:

答案 0 :(得分:4)

使用:

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

它会将你的分割限制为2个字符串

答案 1 :(得分: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中解释的解决方案会更好。

答案 2 :(得分: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();