What's wrong with this Regex.Split

J

Jianwei Sun

string sTest="TEST1||TEST2";
string[] asTest =Regex.Split(sTest, "||" );

I want to get an array with two elements TEST1 and TEST2, but it returs
every char inside the sTest as a seperate array element.

Thanks,
Jianwei
 
J

Jianwei Sun

This will return an array with three elements with one empty string
inside, instead of two. Still, appreciate your reply.
 
T

Tim Wilson

Try escaping the pipes.

string sTest = "TEST1||TEST2";
string[] asTest = Regex.Split(sTest, @"\|\|");
 
A

Adam Clauss

Ahhh.... you're absolutely right - sorry!

--
Adam Clauss
(e-mail address removed)

Jianwei Sun said:
This will return an array with three elements with one empty string
inside, instead of two. Still, appreciate your reply.
Adam said:
Why not just:
string[] asTest =sTest.Split("||".ToCharArray());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top