Query
Query

Reputation: 625

Remove \n from the start of the string c#

I have a string which look like "\n\n\n\ABC\n XYZ", i just want to remove all my new line character from starting of the string.

string replacement = Regex.Replace(s, @"\n", "");

If i use this one so it will remove all new line from string which i don't want.

Upvotes: 3

Views: 1515

Answers (1)

Martin
Martin

Reputation: 16423

If you are just looking to remove newline characters from the start of a string, why not use TrimStart:

string s = "\n\n\n\nABC\n XYZ";
string replacement = s.TrimStart('\n');
Console.WriteLine(replacement);

This will remove all the leading \n characters from the string. Output:

ABC
 XYZ

Upvotes: 13

Related Questions