views:

6138

answers:

9

How do I replace \n with empty space? i get an empty literal error if I do this:

string temp = mystring.Replace('\n', '');
+1  A: 

This should work.

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

Are you sure there are actual \n new lines in your original string?

Robin Day
A: 
string temp = mystring.Replace("\n", " ");
BFree
+10  A: 

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

If you use the String.Replace(string, string) override, it should work.

string temp = mystring.Replace("\n", "");
Samuel
pls read my notes. \n does not get removed with this
gnomixa
It must, otherwise you are missing the \r of the new line or not properly storing the returned value from Replace.
Samuel
+1  A: 

One caveat: in .NET the linefeed is "\r\n". So if you're loading your text from a file, you might have to use that instead of just "\n"

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

SnOrfus
-1, "\r\n" is not from .NET. It's from Windows, use Environment.NewLine to find the newline sequence for the environment (Windows/Linux/Other).
Samuel
Either way, the point still stands. "\n" is not necessarily the linefeed.
SnOrfus
And "\n" is not not necessarily the linefeed. Since the poster has it, one must infer that it is the linefeed they are looking for.
Samuel
For such an inference to be valid and true the premise (in this case, that \n is what the poster wants and needs) must also be true. If that were the case, it'd work just fine as it is.
SnOrfus
/agree with SnOrfus, if the OP has tried String.Replace("\n", "") and it doesn't work, then the string must contain "\r\n"s
Patrick McDonald
Stop arguing about \r\n and use Environment.NewLine
Benjol
+2  A: 

If you use

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

then you won't have to worry about where your string is coming from.

Patrick McDonald
Shouldn't that be mystring.Replace("\r\n", "").Replace("\n", "") ?If you replace the \n with "", then "\r\n" becomes "\r" and "\r\n" won't match anything.
chris
+1  A: 

What 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 that wherever you want you can do this.

s.ReplaceTHAT();

Best Regards!

MRFerocius
A: 

@gnomixa - What do you mean in your comment about not achieving anything? The following works for me in VS2005.

If your goal is to remove the newline characters, thereby shortening the string, 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 to be passed to Replace(string,char)

JeffH
+2  A: 

As replacing "\n" with "" doesn't give you the result that you want, that means that what you should replace is actually not "\n", but some other character combination.

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

If the source of the string is system specific you should use that specific string, otherwise you should use Environment.NewLine to get the newline character combination for the current system.

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

or:

string temp = mystring.Replace(Environment.NewLine, string.Empty);
Guffa
A: 

it turned out that the string contained \r, so I am replacing both

replace("\n", ""); replace("\r", "");
gnomixa