2

How to remove \n in a string?

E.g.

  1. Input : string s = "\n1234"
  2. Output I want (but I can't achieve) : string s = "1234"

What I've tried (and failed) :

  1. s.Remove(0,1);
  2. s.Trim();

Additional notes :

I think \n counts as 1 char because, when I tried :

bool b = s[0]=='\n';

b = true.

Dmitry
  • 13,797
  • 6
  • 32
  • 48
Moses Aprico
  • 1,951
  • 5
  • 30
  • 53
  • 1
    you are not assigning the result back to `s`. `s.Trim()` should work like `s = s.Trim()` – Habib May 14 '14 at 14:08
  • 3
    `Trim` and `Remove` don't modify the string in-place; they return a new string. Are you re-assigning `s` after you call those methods? – Rex M May 14 '14 at 14:08
  • '\' is an escape character. Followed by 'n' that means 'newline character'. The two characters you typed in are turned into the single character by your compiler when it reads the source file. – Joe May 14 '14 at 14:08
  • Or watch [this](http://stackoverflow.com/questions/4140723/how-to-remove-new-line-characters-from-a-string) – Alex Skiba May 14 '14 at 14:10

4 Answers4

7

Maybe not the fastes solution, but works:

string s = "\n1234".Replace("\n", String.Empty);

If the \n is only at the beginning or end of the string you can also use a "custom Trim":

string s = "\n1234".Trim('\n');

The '\n' should not be required as it is included in the default white-space characters, which are removed by Trim by default.

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
  • 2
    `"\n1234".Replace(@"\n"` is not right. It's trying to replace a newline by looking for a literal `\n`. It's actually a newline, so using `@` is going to be more trouble than good. – Tim S. May 14 '14 at 14:19
  • Thanks, Snake fooled my ;) - you are right. This time I tested the code and `Replace("\n", String.Empty)` does work... – Christoph Fink May 14 '14 at 14:21
  • it seems i keep forgetting to assign trim return value. sigh. thanks! – Moses Aprico May 14 '14 at 14:30
3

You are not assigning the result back to string, otherwise your both Trim and Remove should work.

String is an immutable type, and its method doesn't modify the exiting string, instead they return a new string. So when you do s.Remove(0, 1); or s.Trim(), it doesn't modify the original string, instead it returns a new string.

So for your case something like:

string newstr = s.Trim();
//OR
string newstr = s.Remove(0,1); 

should work.

But remember s.Trim() would remove any kind of white space from beginning or end of string. If that is your desired behaviour use that.

Similarly you can use string.Replace but that will replace all occurrence of new line character from the string.

And your s.Remove(0,1) will remove the first character irrespective of whether it is a new line character or not.

Habib
  • 219,104
  • 29
  • 407
  • 436
1

Give this a shot:

string s = "\n1234";

s = s.Replace("\n", string.Empty);
Ian P
  • 12,840
  • 6
  • 48
  • 70
0

You can use:

string s = "\n1234".TrimStart(new char[]{'\n'}); // search for \n at the start of the string

or

string s = "\n1234".TrimEnd(new char[]{'\n'});// search for \n at the end of the string

or

string s = "\n1234".Trim(new char[]{'\n'});// search for \n at the start and the end of the string