-1

I want to split these new lines into one line replace them with commas.

output now=

 s = Ar,TB,YY 
    
    0,2022323,02
    
    0,223233,29

output I want:

s = Ar,TB,YY,0,2022323,02,0,223233,29;

I tried using s = s.replaceAll("\n", ","); , didnt seem to make a difference.

dsknjksad
  • 83
  • 4

1 Answers1

2

Since Java 8 we can use in regex \R which represents line separator which aside from \n can also be \r, or \r\n pair and more.

So your code can look like

s = s.replaceAll("\\R+", ",");
  • "\\R" to represents \R in regex
  • + to match one or more line separator, in case you want to remove more than one empty lines with single comma ,
Pshemo
  • 122,468
  • 25
  • 185
  • 269