How to make multiple lines of lines one line?

I have below the line

String str="select * from m_menus;

select * from m_roles";

I want the line above on the same line as

String str="select * from m_menus;select * from m_roles";

I tried

str1=str.replace("[\r\n]+", " "); 

and

str1=str.replace("\n"," "); 

Both do not work.

+5
source share
4 answers

If you want to use a regex, you must use a method String.replaceAll().

+6
source

Use String.replaceAllinstead.

str1=str.replaceAll("[\r\n]+", " ");
+12
source

Why aren't you using str.replaceAll("\r\n", " ")?

Should work and replace all occurrences.

+3
source

There are no regular expressions and an operating system independently:

str1.replaceAll(System.lineSeparator(), " ");

Windows uses \ r \ n as a line breaker, and on * nix systems only \ n is used.

0
source

All Articles