-1

I want to remove all empty linse in Java. In Eclipse I will use:

\n( *)\n (or "\r\n( *)\r\n" in Windows)

. But in Java it isn't work (I used:

str=str.replaceAll("\n( *)\n")

). How to do it in Java using replaceAll? Sample:

package example
○○○○
public ... (where ○ is space)
barwnikk
  • 950
  • 8
  • 14

1 Answers1

1

I would do it like this

java.util.regex.Pattern ws = Pattern.compile("[\r|\n][\\s]*[\r|\n]");
java.util.regex.Matcher matcher = ws.matcher(str);
str = matcher.replaceAll(" ");
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249