Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make multiple line string to single line string?

Tags:

java

java-11

I have below string

String str="select * from m_menus;

select * from m_roles";

I want above string in one line like

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

I have tried

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

and also

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

Both are not working.

like image 831
happy Avatar asked Jun 27 '12 07:06

happy


People also ask

How do you convert multiple lines to one line?

Select the lines you want to join ( Ctrl + A to select all) Choose Edit -> Line Operations -> Join Lines.

How do I convert a string to one line?

Step 1: Be prepared with the text which you want to convert. Step 2: Copy the text, JSON, String, Pdf or XML (whichever you want to convert). Step 3: Paste the copied content in the space provided. Step 4: Finally click the "convert" option to get the text converted in a single line.

How do I put multiple lines on one line in Excel?

You can put multiple lines in a cell with pressing Alt + Enter keys simultaneously while entering texts. Pressing the Alt + Enter keys simultaneously helps you separate texts with different lines in one cell.

Can a string be multiple lines?

Raw StringsThey can span multiple lines without concatenation and they don't use escaped sequences. You can use backslashes or double quotes directly.


4 Answers

No regular expressions and operating system independent:

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

Windows uses \r\n as a line breaker, while *nix systems use only \n.

like image 153
dfinki Avatar answered Oct 10 '22 15:10

dfinki


Use String.replaceAll instead.

str1=str.replaceAll("[\r\n]+", " ");
like image 42
Mattias Buelens Avatar answered Oct 10 '22 16:10

Mattias Buelens


If you want to use regex, you should use the String.replaceAll() method.

like image 6
TZHX Avatar answered Oct 10 '22 14:10

TZHX


Why don't you use str.replaceAll("\r\n", " ") ?

Should work and replace all occurences.

like image 3
Michael Laffargue Avatar answered Oct 10 '22 16:10

Michael Laffargue