替换字符串中的换行符 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/238002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace Line Breaks in a String C#
提问by YonahW
How can I replace Line Breaks within a string in C#?
如何在 C# 中的字符串中替换换行符?
采纳答案by Corin Blaikie
Use replace with Environment.NewLine
使用替换为 Environment.NewLine
myString = myString.Replace(System.Environment.NewLine, "replacement text"); //add a line terminating ;
As mentioned in other posts, if the string comes from another environment (OS) then you'd need to replace that particular environments implementation of new line control characters.
正如其他帖子中提到的,如果字符串来自另一个环境 (OS),那么您需要替换新行控制字符的特定环境实现。
回答by The.Anti.9
Use the .Replace() method
使用 .Replace() 方法
Line.Replace("\n", "whatever you want to replace with");
回答by Brian R. Bondy
I would use Environment.Newline when I wanted to insert a newline for a string, but not to remove all newlines from a string.
当我想为字符串插入换行符但不想从字符串中删除所有换行符时,我会使用 Environment.Newline。
Depending on your platform you can have different types of newlines, but even inside the same platform often different types of newlines are used. In particular when dealing with file formats and protocols.
根据您的平台,您可以使用不同类型的换行符,但即使在同一平台内,也经常使用不同类型的换行符。特别是在处理文件格式和协议时。
string ReplaceNewlines(string blockOfText, string replaceWith)
{
return blockOfText.Replace("\r\n", replaceWith).Replace("\n", replaceWith).Replace("\r", replaceWith);
}
回答by ZombieSheep
To extend The.Anyi.9's answer, you should also be aware of the different types of line break in general use. Dependent on where your file originated, you may want to look at making sure you catch all the alternatives...
要扩展 The.Anyi.9 的答案,您还应该了解一般使用中不同类型的换行符。根据您的文件的来源,您可能需要查看确保您捕获所有替代品...
string replaceWith = "";
string removedBreaks = Line.Replace("\r\n", replaceWith).Replace("\n", replaceWith).Replace("\r", replaceWith);
should get you going...
应该让你去...
回答by driis
If your code is supposed to run in different environments, I would consider using the Environment.NewLine
constant, since it is specifically the newline
used in the specific environment.
如果您的代码应该在不同的环境中运行,我会考虑使用Environment.NewLine
常量,因为它专门newline
用于特定环境。
line = line.Replace(Environment.NewLine, "newLineReplacement");
However, if you get the text from a file originating on another system, this might not be the correct answer, and you should replace with whatever newline constant is used on the other system. It will typically be \n
or \r\n
.
但是,如果您从源自另一个系统的文件中获取文本,这可能不是正确答案,您应该替换为其他系统上使用的任何换行符常量。它通常是\n
或\r\n
。
回答by tvanfosson
Don't forget that replace doesn't do the replacement in the string, but returns a new string with the characters replaced. The following will remove line breaks (not replace them). I'd use @Brian R. Bondy's method if replacing them with something else, perhaps wrapped as an extension method. Remember to check for null values first before calling Replace or the extension methods provided.
不要忘记,replace 不会在字符串中进行替换,而是返回一个替换了字符的新字符串。以下将删除换行符(而不是替换它们)。如果用其他东西替换它们,我会使用@Brian R. Bondy 的方法,也许包装为扩展方法。请记住在调用 Replace 或提供的扩展方法之前先检查空值。
string line = ...
line = line.Replace( "\r", "").Replace( "\n", "" );
As extension methods:
作为扩展方法:
public static class StringExtensions
{
public static string RemoveLineBreaks( this string lines )
{
return lines.Replace( "\r", "").Replace( "\n", "" );
}
public static string ReplaceLineBreaks( this string lines, string replacement )
{
return lines.Replace( "\r\n", replacement )
.Replace( "\r", replacement )
.Replace( "\n", replacement );
}
}
回答by tvanfosson
string s = Regex.Replace(source_string, "\n", "\r\n");
or
或者
string s = Regex.Replace(source_string, "\r\n", "\n");
depending on which way you want to go.
取决于你想走哪条路。
Hopes it helps.
希望它有帮助。
回答by data
Best way to replace linebreaks safely is
安全替换换行符的最佳方法是
yourString.Replace("\r\n","\n") //handling windows linebreaks
.Replace("\r","\n") //handling mac linebreaks
that should produce a string with only \n (eg linefeed) as linebreaks. this code is usefull to fix mixed linebreaks too.
这应该产生一个只有 \n (例如换行符)作为换行符的字符串。此代码对于修复混合换行符也很有用。
回答by Matt Hinze
var answer = Regex.Replace(value, "(\n|\r)+", replacementString);
回答by Zamir
I needed to replace the \r\n
with an actual carriage return and line feed and replace \t
with an actual tab. So I came up with the following:
我需要\r\n
用实际的回车和换行替换\t
并用实际的选项卡替换。所以我想出了以下内容:
public string Transform(string data)
{
string result = data;
char cr = (char)13;
char lf = (char)10;
char tab = (char)9;
result = result.Replace("\r", cr.ToString());
result = result.Replace("\n", lf.ToString());
result = result.Replace("\t", tab.ToString());
return result;
}