N
N
nuclear_kote2018-05-10 12:26:55
Java
nuclear_kote, 2018-05-10 12:26:55

Why doesn't the \n substitution work?

System.out.println("GET http://www.example.com/example.js HTTP/1.1\n" +
                "Host: www.example.com\n" +
                "Proxy-Connection: keep-alive\n" +
                "If-Modified-Since: Wed, 06 Jul 2016 07:58:20 GMT\n\n".replaceAll("\n", "E"));

as a result, only the last 2 characters are replaced.
Is there a more normal solution other than something like this:
Pattern pattern = Pattern.compile("\n", Pattern.MULTILINE);
        System.out.println(pattern.matcher("GET http://www.example.com/example.js HTTP/1.1\n" +
                "Host: www.example.com\n" +
                "Proxy-Connection: keep-alive\n" +
                "If-Modified-Since: Wed, 06 Jul 2016 07:58:20 GMT\n\n").replaceAll("E"));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Pugachev, 2018-05-10
@nuclear_kote

I'm not a javist in its purest form, but I know that .replace() is performed first and then "+"
use StringBuffer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question