String.Replace(char, char) method in C#

C#String

C# Problem Overview


How do I replace \n with empty space?

I get an empty literal error if I do this:

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

C# Solutions


Solution 1 - C#

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", "");

Solution 2 - C#

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);

Solution 3 - C#

This should work.

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

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

Solution 4 - C#

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.

Solution 5 - C#

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.

Solution 6 - C#

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.

Solution 7 - C#

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!

Solution 8 - C#

Here is your exact answer...

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

But this one is much better... Specially if you are trying to split the lines (you may 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);

Solution 9 - C#

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

Solution 10 - C#

@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)

Solution 11 - C#

Found on Bytes.com:

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

Solution 12 - C#

I know this is an old post but I'd 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;
    }

Example usage:

string newText = Replace("This is an \r\n \n an example.", new string[] { "\r\n", "\n" }, "");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsarsnakeView Question on Stackoverflow
Solution 1 - C#SamuelView Answer on Stackoverflow
Solution 2 - C#GuffaView Answer on Stackoverflow
Solution 3 - C#Robin DayView Answer on Stackoverflow
Solution 4 - C#Shawn KovacView Answer on Stackoverflow
Solution 5 - C#Patrick McDonaldView Answer on Stackoverflow
Solution 6 - C#Steven EversView Answer on Stackoverflow
Solution 7 - C#MRFerociusView Answer on Stackoverflow
Solution 8 - C#cilerlerView Answer on Stackoverflow
Solution 9 - C#BFreeView Answer on Stackoverflow
Solution 10 - C#JeffHView Answer on Stackoverflow
Solution 11 - C#VictorView Answer on Stackoverflow
Solution 12 - C#DeejddView Answer on Stackoverflow