views:

24886

answers:

10

How can I replace Line Breaks within a string in C#?

A: 

Use the .Replace() method

Line.Replace("\n", "whatever you want to replace with");
The.Anti.9
+4  A: 

I would use Environment.Newline when I wanted to insert a newline for a string, but not to remove all newlines from a string.

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);
}
Brian R. Bondy
+7  A: 

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...

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

should get you going...

ZombieSheep
+30  A: 

Use replace with environment.newline

myString = myString.Replace(Environment.Newline, "replacement text")

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.

Corin
I knew there had to be a better than strText.Replace("\r","\n").Replace("\n\n","\n").Replace("\r","<br>");Thanks!
x13
There is a small typo in the code. The L in NewLine needs to be capitalized: Environment.NewLine
Peter Jacoby
+8  A: 

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.

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.

driis
You need to reassign it back to the original variable as replacement doesn't occur in place.
tvanfosson
+3  A: 

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.

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 );
   }
}
tvanfosson
can't have `''` in C# - there is no such thing as an empty char. will `'\0'` work instead?
Shevek
@Shevek -- just used the wrong quotes. Must have been doing a fair amount of javascript the day I answered this.
tvanfosson
A: 
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.

Freddy
A: 

now i know what to do... thanks

Filipe Teixeira
Please create a new question – do not hitch-hike an old one. And please make the question clearer. At the moment it’s rather hard to understand.
Konrad Rudolph
No, the \r as `'\r'` or `"\r"` is interpreted by C# as a special character. To use the literal \r you would have to escape the backslash with another one, e.g. `'\\r'`
ck
A: 

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.

data_smith
A: 
var answer = Regex.Replace(value, "(\n|\r)+", replacementString);
Matt Hinze