Remove \n from the start of the string c#

Viewed 968

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.

1 Answers

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
Related