-1

I'm trying to trim a string of some character so I can put it in an SQL query. An example of what is being passed to my method is "\n asdw_value\n". I want the "\n"s and the space to be removed, but I can't get the "\n" to go away. My code now looks like this :

element = element.Replace("\\", "");
element = element.Replace("n", "");
element = element.Replace(" ", "");

And the output has been "\nasdq_value\n" Any help is appreciated.

Danger_Fox
  • 449
  • 2
  • 11
  • 33

1 Answers1

1

Most likely the string value you're seeing is produced in the debugger. The \n is an escape sequence meaning 'newline'. It's a single character, not a \ followed by a n, so to remove it from your input, you will need to use the same escape sequence, like this:

element = element.Replace("\n", "");
element = element.Replace(" ", "");

If you only want to trim these characters from the beginning and end of the string, you can do this:

element = element.Trim(new char[] { '\n', ' ' });
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331