简体   繁体   English

从字符串c#的开头删除\\ n

[英]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. 我有一个看起来像“\\ n \\ n \\ n \\ ABC \\ n XYZ”的字符串,我只是想从字符串的开头删除我的所有新行字符。

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. 如果我使用这个,那么它将删除我不想要的字符串中的所有新行。

If you are just looking to remove newline characters from the start of a string, why not use TrimStart : 如果您只是想从字符串的开头删除换行符,为什么不使用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. 这将从字符串中删除所有前导\\n字符。 Output: 输出:

ABC
 XYZ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM