0

i am getting cookie value as parameter .I need to split the cookie value(name,value).

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

It's even taking the last equal character also.I need following output.

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

It should consider only the first equal character.after that it shouldn't spilt the string

3 Answers 3

4

use:

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

it will limit your split to 2 strings

Sign up to request clarification or add additional context in comments.

Comments

1
string Cookie ="secureToken_sdfsdfbsadbfsdhbh = Tdne68JhO5baix2Ey_K0ICV1HAAFvUP5BA==";
string[] cookieData = Cookie.Split(new string[] {" = "}, StringSplitOptions.RemoveEmptyEntries);

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

Output will be;

secureToken_sdfsdfbsadbfsdhbh
Tdne68JhO5baix2Ey_K0ICV1HAAFvUP5BA==

Here a Demonstration.

EDIT: Since you only want 2 split part, limiting will be better as a solution explained in wudzik answers.

1 Comment

I think using string.Split with count parameter is a bit cleaner solution, but this is a good answer though :)
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();

1 Comment

same solution as Soner's, different syntax, new answer should add something

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.