Split method C# 2013


i am trying to split a value using an array and split function but i cant get it to work,
the array returns the whole value.

any help would be appreciated

my code:

dir = "1\n\n\n";

string[] parts = dir.Split('\\');
Joseph KrauszCEOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
\ is a special character in C#. You need to use:
                string dir = @"1\n\n\n";
                string[] parts = dir.Split(new char[] { Convert.ToChar(@"\") });
Joseph KrauszCEOAuthor Commented:
thanks for reply

i forgot to tell that the actual value i am getting from a function so i don't have to use the "@" which usually adds a 2nd "\", so this might be the issue
please advise,

Jacques Bourgeois (James Burger)PresidentCommented:
Escape sequences are considered only in string literals, they are not interpreted in the value of a variable, so you should not get the problem if a backslash appears in the value of a String.

You have the problem in your example because you assign the value to dir with a literal in the code, where the \n is seen as an escape sequence. So what you assign to dir is 1 followed by 3 newline characters.

If you were to assign the value as dir = "1\\n\\n\\n", your variable would receive a series of backslash followed by n instead of the newlines, and the split would work correctly.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Joseph KrauszCEOAuthor Commented:
the problem is that i am getting this result from code i didn't built so i its hard for me to find the core of the problem, but i got over it with using Regex.escape


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Joseph KrauszCEOAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for yidel718's comment #a39572249

for the following reason:

because this is the answer
Jacques Bourgeois (James Burger)PresidentCommented:
The question as asked was answered.

Only in his closing comment did yidel state that it was coming from an uncontrollable source. We would have oriented the discussion otherwise if we had that detail.
Joseph KrauszCEOAuthor Commented:
i want to give the whole credit for JamesBurger, as he answered the question as is, but my bad i wasn't clear enough, and the reason because i am relative new to .net, coming from access but please leave the conversation as is, that other people like me having this problem should get the solution,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.