13

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.

Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
happy
  • 2,550
  • 17
  • 64
  • 109
  • 2
    Stupid question.. but why don't you just do this in the first place?: `String str="select * from m_menus;select * from m_roles";` Is it really the string that's the problem or are you passing it to a SQL engine and getting results you're not happy with. – forsvarir Jun 27 '12 at 07:43
  • @forsvarir Probably he is getting string in that manner from user input? – Mukund Samant Jun 27 '12 at 07:44
  • I am taking string as input from user – happy Jun 27 '12 at 07:53

7 Answers7

20

Use String.replaceAll instead.

str1=str.replaceAll("[\r\n]+", " ");
Mattias Buelens
  • 19,609
  • 4
  • 45
  • 51
10

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.

dfinki
  • 324
  • 4
  • 13
6

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

TZHX
  • 5,291
  • 15
  • 47
  • 56
  • 3
    Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such. – TZHX Jun 27 '12 at 07:47
3

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

Should work and replace all occurences.

Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76
1

Using Java 11 lines()

Java 11 has added new method String::lines, lines method uses specialized Spliterator to lazily provide lines from the source string

yourMultilineString.lines().collect(Collectors.joining(";"));

Note The string uses Windows’ \r\n and even though I’m on Linux, lines() still splits it. That’s because regardless of the operating system, the method treats \r, \n, and \r\n as line terminators and splits there – even if they are mixed in the same string.

Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
0
String actualTooltip_3 = string.replaceAll("[\r\n]+", " ");

/////////--------how to make multiple line string to single line string?--------//////// ///string - value having multi lines

///actualTooltip_3 - Value shown in a single line

enter image description here

enter image description here

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 12 '22 at 13:45
-1
str1=str.replaceAll("[\r\n]+", " ");
Naresh
  • 16,698
  • 6
  • 112
  • 113
  • try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer – Agilanbu Jan 02 '19 at 06:37