5

Basically, all I'm trying to do is remove the newline character from a String in Java. I've looked at dozens of posts asking similar questions and they all say the same thing, but none of them seem to work for me. Here is all the code I have 3 lines:

    String text = "\\n          texttexttext     \\n";
    text = text.replaceAll("\\n", "");
    System.out.println(text);

That string is similar to what I'm actually trying to use, but even with this one I can't find the newline character and replace it. The replaceAll just won't see it and I don't know why.

I've tried plenty of other things too like

    text = text.replaceAll("\\n", "").replaceAll("\\r", "");

or

    text = text.replaceAll("\\r\\n|\\r|\\n", " ");

but nothing can even find the character. I haven't even been able to find it using Regex Pattern and Matcher objects. The only thing slightly unusual I'm doing is doing it in a Junit Test bean, but I cannot believe that would do anything.

3
  • You are printing txt not text - is that intended? Commented Feb 6, 2015 at 19:57
  • 3
    String text = "\\n texttexttext \\n";, "\\n" here isn't a newline, it is literally a "\n". Commented Feb 6, 2015 at 19:58
  • Yeah, sorry. the name isn't the problem. I just changed some of my variable names in the post for clarity and made a mistake with one, I'll fix that. But I don't think your suggesting is fixing it for me. I've tried it with using "\\n", "\n", and even "\\\n" but nothing can find it. Commented Feb 6, 2015 at 20:00

6 Answers 6

9

You don't have a newline character in your original text. You have escaped the backslash, not the n, so you have actual backslash \ and n characters in your text string.

You did have to escape the backslash character in your regular expression, but not in your literal string text.

If you initialize text as "\n texttexttext \n", then it will find and replace those newlines as expected.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, that does seem to work, but I don't understand why I wouldn't be able to find the String "\\n" then. What I'm actually trying to parse is a large document so I think I would be able to have to find "\\n" in that and then change them to "\n". But therein lies the problem with finding a "\\n" in a String, for me anyway.
To find \\n in a string, you'd need a regular expression that includes two backslash characters before the n, both of which also need to be escaped for Java itself -- "\\\\n".
4
String newLine = System.getProperty("line.separator")
System.out.println(newLine.contains("\n")); // this is a new line
System.out.println(newLine.contains("\\n"));

output:

true
false

Comments

3

As commented by Jon Lin

Your example text does not actually talk about newlines - \\n is the string \n.

The other fallacy is that replaceAll expects a regular expression as the first input. So \\n is actually replaced to \n as \ is the escaping character, which is then interpreted as a newline character - case of double escaping, thus in your input text \\ is not matched.

If you try

text = text.replaceAll("\\\\n", "");

At least you get the expected result, as it's both a Java string and a regular expression both interpret \ as escaping character.

2 Comments

\n is a defined Regex character. The problem is just, that there is no new line in the string.
Wow, that worked. I don't know why I didn't try that before actually. I wasted almost 2 hours on this, thanks.
0

In addition to @rgettman's answer and in order to make it useful for the community, below is an awesome Java program (For more informations, you can read the entire article from here.) that replaces newline charachters.

public class StringReplaceTask{


    public static void main(String args[]) {

        // Removing new line characters form Java String
        String sample="We are going to replace newline character \\n "
                + "New line should start now if \\n \n is working";

        System.out.println("Original String: " + sample);

        //replacing \n or newline character so that all text come in one line
        System.out.println("Escaped String: " + sample.replaceAll("\n", ""));


        // removing line breaks from String read from text file in Java
        String text = readFileAsString("words.txt");
        text = text.replace(System.getProperty("line.separator"), "");

    }  

Comments

0

Since Java 8

You can do it as follows

text.replaceAll("\\R+", " ")

Comments

-3

if head = 'Hello' but when you print(head) -> 'Hello\n' You can remove \n by rstrip() or remove()

you can use my code

**head=a[0:(len(head)-1)]**

to remove \n hidden in string.

(test on py 3) good luck!!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.