2008-10-26 61 views
419

如何替换C#中字符串中的换行符?替换字符串C中的换行符#

+1

请告诉我们更多:什么是您的“换行”?你想用什么替换它们? – 2008-10-26 13:55:29

+0

哈哈。我在java中检查相同的时候,当我发现System.getProperty(“line.separator”)是古怪的知道在C#中的对手。你的文章帮助了我。 – Ravisha 2010-09-29 04:49:39

+0

可能的重复[什么是从C#中的字符串中删除换行符的最快方法?](http://stackoverflow.com/questions/25349/what-would-be-the-fastest-way-to-remove-换行符) – 2011-01-19 18:56:59

回答

592

使用替换Environment.NewLine

myString = myString.Replace(System.Environment.NewLine, "replacement text") 

正如其他帖子所述,如果字符串来自另一个环境(OS),那么您需要替换new line control characters的特定环境实现。

2

使用.Replace()方法

Line.Replace("\n", "whatever you want to replace with"); 
31

我会用Environment.Newline当我想插入新行的字符串,而不是从一个字符串中删除所有换行符。

根据您的平台,您可以有不同类型的换行符,但即使在同一个平台内,通常也会使用不同类型的换行符。特别是在处理文件格式和协议时。

string ReplaceNewlines(string blockOfText, string replaceWith) 
{ 
    return blockOfText.Replace("\r\n", replaceWith).Replace("\n", replaceWith).Replace("\r", replaceWith); 
} 
+0

当我想将检索到的网页代码折叠到一行时(这使得正则表达式模式更容易),这是唯一对我有用的东西。 – 2011-07-17 19:09:03

+0

完全同意Brian R. Bondy。至少,Corin提供的解决方案非常幼稚。 – Califf 2013-03-29 21:33:42

155

要扩展The.Anyi.9的答案,您应该注意different types of line break in general use。依赖于你的文件来自于哪里,你可能想看看确保你抓住所有的可能性...

string replaceWith = ""; 
string removedBreaks = Line.Replace("\r\n", replaceWith).Replace("\n", replaceWith).Replace("\r", replaceWith); 

应该让你去...

+19

+1此解决方案更完整。 – 2011-03-13 00:51:46

+5

首先,我更喜欢环境thingi,但如果字符串does not来自系统运行,它不会工作。 +1 – Flo 2011-11-17 10:21:03

17

如果您的代码应该运行在不同的环境中,我会考虑使用Environment.NewLine常量,因为它特别是在特定环境中使用的newline

line = line.Replace(Environment.NewLine, "newLineReplacement"); 

但是,如果你得到源自另一个系统上的文件中的文本,这可能不是正确的答案,你应该用什么换行不变的是其他系统上使用替代。它通常是\n\r\n

13

不要忘记替换不会在字符串中进行替换,但会返回一个替换字符的新字符串。以下将删除换行符(而不是替换它们)。如果用别的东西代替它们,我会使用@Brian R. Bondy的方法,也许将其作为扩展方法进行包装。记得在调用Replace或提供的扩展方法之前先检查空值。

string line = ... 

line = line.Replace("\r", "").Replace("\n", ""); 

由于扩展方法:

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); 
    } 
} 
0
string s = Regex.Replace(source_string, "\n", "\r\n"); 

string s = Regex.Replace(source_string, "\r\n", "\n"); 

这取决于你想要的方式去。

希望它有帮助。更换换行符

2

最好的方法是安全

yourString.Replace("\r\n","\n") //handling windows linebreaks 
.Replace("\r","\n")    //handling mac linebreaks 

应该产生一个字符串,只有\ n(如换行符)作为换行符。 这段代码对于修复混合的linebreaks也很有用。

2
var answer = Regex.Replace(value, "(\n|\r)+", replacementString); 
6

我需要与实际的回车和换行来替换\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; 
} 
336

到目前为止发布的解决方案,要么只能更换Environment.NewLine或该替换字符串包含换行符,因为他们叫string.Replace多次他们失败了。

下面是一个解决方案,它使用一个正则表达式在字符串中进行一次遍历所有三个替换。这意味着替换字符串可以安全地包含换行符。

string result = Regex.Replace(input, @"\r\n?|\n", replacementString); 
1

由于新的生产线可以通过\n\r\r\n分隔,首先我们会更换\r\r\n\n,然后才分裂数据串。

以下几行应该去parseCSV方法:

function parseCSV(data) { 
    //alert(data); 
    //replace UNIX new lines 
    data = data.replace(/\r\n/g, "\n"); 
    //replace MAC new lines 
    data = data.replace(/\r/g, "\n"); 
    //split into rows 
    var rows = data.split("\n"); 
} 
8

为了确保换行符(在Windows,Mac和Unix)的所有可能的方式被替换,你应该使用:

string.Replace("\r\n", "\n").Replace('\r', '\n').Replace('\n', 'replacement'); 

并按照这个顺序,当你找到线结尾字符的组合时,不要多余的换行符。

2

为什么不是两个?

string ReplacementString = ""; 

Regex.Replace(strin.Replace(System.Environment.NewLine, ReplacementString), @"(\r\n?|\n)", ReplacementString); 

注:替换strin与你输入字符串的名称。

0

另一种选择是在所讨论的字符串上创建一个StringReader。在阅读器上,循环执行.ReadLine()。然后,无论分隔符是什么(一致或不一致),都会分隔线条。有了这个,你可以按你的意愿行事。一种可能性是使用StringBuilder并在其上调用.AppendLine

好处是,你让框架决定什么构成“换行”。