【发布时间】:2016-04-14 13:21:24
【问题描述】:
我花了很多很多时间寻找答案。如果您在此站点上查找删除新行,它会给出看起来可行的答案,但我无法让它们起作用。
string TextFileBlock = File.ReadAllText("TextFile.txt");
char newlinechar = '\n' ;
TextFileBlock = TextFileBlock.Replace(" ", String.Empty); //works for the spaces
TextFileBlock = TextFileBlock.Replace(newlinechar.ToString(), String.Empty);
///Does not get rid of the newlines. The enter key.
// TextFileBlock = TextFileBlock.Replace("\n", String.Empty);// not works
//TextFileBlock.Replace(Environment.NewLine, string.Empty);//not works
C#问题
【问题讨论】:
-
您知道在 Windows 上,新行由回车和换行符
\r\n组成,而不仅仅是\n?如果您对文件中的字符有疑问,请在十六进制编辑器中查看它,例如 HxD。 -
按照这种逻辑,OP 代码应该删除了 \n 并留下了 \r。