• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

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('\\');
  • 4
  • 2
2 Solutions
É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(@"\") });
yidel718Author 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.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

yidel718Author 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

yidel718Author 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.
yidel718Author 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,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now