String.Replace (char, char) in C #

How to replace \n with empty space?

I get an empty literal error if I do this:

 string temp = mystring.Replace('\n', ''); 
+68
string c #
Mar 16 '09 at 18:56
source share
12 answers

String.Replace('\n', '') does not work because '' not a valid character literal.

If you are using an override of String.Replace (string, string), it should work.

 string temp = mystring.Replace("\n", ""); 
+115
Mar 16 '09 at 19:01
source share

As replacing "\ n" with "" does not give you the result you want, this means that what you need to replace is not really "\ n", but some other combination of characters.

One possibility is that you must replace the character combination "\ r \ n", which is the newline code on a Windows system. If you replace only the character "\ n" (line feed), it will leave the character "\ r" (carriage return), which can still be interpreted as a line break, depending on how you display the line.

If the string source is systemic, you must use this particular string, otherwise you must use Environment.NewLine to get the newline combination for the current system.

 string temp = mystring.Replace("\r\n", string.Empty); 

or

 string temp = mystring.Replace(Environment.NewLine, string.Empty); 
+15
Mar 16 '09 at 19:57
source share

That should work.

 string temp = mystring.Replace("\n", ""); 

Are you sure there are real \ n newlines in the source line?

+4
Mar 16 '09 at 18:59
source share
 string temp = mystring.Replace("\n", string.Empty).Replace("\r", string.Empty); 

Obviously, this removes both "\ n" and "\ r" and is as simple as I know how to do it.

+4
Nov 15 '13 at 15:12
source share

If you use

 string temp = mystring.Replace("\r\n", "").Replace("\n", ""); 

You don’t have to worry about where your line is coming from.

+2
Mar 16 '09 at 19:32
source share

Found on Bytes.com :

string temp = mystring.Replace('\n', '\0');// '\0' represents an empty char

+2
Oct 11
source share

One caveat: in .NET, the string returns "\ r \ n". Therefore, if you are loading text from a file, you may need to use it instead of "\ n"

edit> as pointed out in the comments on samuel, "\ r \ n" is not specific to .NET, but is specific to Windows.

+1
Mar 16 '09 at 19:02
source share

How about creating an extension method like this ....

  public static string ReplaceTHAT(this string s) { return s.Replace("\n\r", ""); } 

And then, when you want to replace it, wherever you want, you can do it.

 s.ReplaceTHAT(); 

Regards!

+1
Mar 16 '09 at 19:50
source share

Here is your exact answer ...

 const char LineFeed = '\n'; // #10 string temp = new System.Text.RegularExpressions.Regex( LineFeed ).Replace(mystring, string.Empty); 

But this is much better ... Especially if you are trying to split lines (you can also use it with Split)

 const char CarriageReturn = '\r'; // #13 const char LineFeed = '\n'; // #10 string temp = new System.Text.RegularExpressions.Regex( string.Format("{0}?{1}", CarriageReturn, LineFeed) ).Replace(mystring, string.Empty); 
+1
Nov 27 '11 at 4:21
source share
 string temp = mystring.Replace("\n", " "); 
0
Mar 16 '09 at 19:01
source share

@gnomixa - What do you mean in your comment that nothing was achieved? In VS2005, the following works for me.

If your goal is to remove newlines, thereby truncating the line, look at this:

  string originalStringWithNewline = "12\n345"; // length is 6 System.Diagnostics.Debug.Assert(originalStringWithNewline.Length == 6); string newStringWithoutNewline = originalStringWithNewline.Replace("\n", ""); // new length is 5 System.Diagnostics.Debug.Assert(newStringWithoutNewline.Length == 5); 

If your goal is to replace the newline characters with a space character, leaving the string length the same, look at this example:

  string originalStringWithNewline = "12\n345"; // length is 6 System.Diagnostics.Debug.Assert(originalStringWithNewline.Length == 6); string newStringWithoutNewline = originalStringWithNewline.Replace("\n", " "); // new length is still 6 System.Diagnostics.Debug.Assert(newStringWithoutNewline.Length == 6); 

And you have to replace single-character strings instead of characters, because `` is not a valid character, which should be replaced with Replace (string, char)

0
Mar 16 '09 at 19:55
source share

I know this is an old post, but I would like to add my method.

 public static string Replace(string text, string[] toReplace, string replaceWith) { foreach (string str in toReplace) text = text.Replace(str, replaceWith); return text; } 

Usage example:

 string newText = Replace("This is an \r\n \n an example.", new string[] { "\r\n", "\n" }, ""); 
0
Jan 20 '13 at 19:45
source share



All Articles