Snippet: Remove blank lines from text

| 1 minute | Comments

text.replaceAll("(?m)(^\\s*$\\r?\\n)+", "")

Or via Pattern objects:

import java.util.regex.Pattern

Pattern
  .compile("(^\\s*$\\r?\\n)+", Pattern.MULTILINE)
  .matcher(text)
  .replaceAll("")
| Written by
Tags: Java | Scala | Snippet