0

I am using the wikimedia api to get content from wikipedia pages. The api returns a lot of "\n" as plain text. I want to remove them from a string

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

Neither of these work, any ideas?

4

3 Answers 3

2

When your String contains a plaintext \n it is actually a \\n otherwise it would be displayed as a linebreak, which is why I found s = s.replaceAll("\\\\n","") to be working for me. An example snippet:

class Main{
  public static void main(String[] args){
        String s = "Hello\\nHello";
        System.out.println(s);
        s = s.replaceAll("\\\\n","");
        System.out.println(s);
    }
}

Remember that replaceAll takes a Regex: Since you want to replace 2 /s you have to escape both of them, therefore////

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

Comments

0

Hi Please to use below code format:

s= s.replace("\n", "").replace("\r", "");

Thanks

Comments

0

You can use the code below:

s = s.replace("\n", "");

but, the newline character can be different among the environments. So, you can use this

s = s.replace(System.getProperty("line.separator"), "");

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.