fork download
  1. import java.util.*;
  2.  
  3. class Ideone {
  4. public static void main(String[] args) {
  5. String multiLineText = "\tHello World!" + "\n"
  6. + "New line";
  7.  
  8. String lineSeparatorRegex = "\r?\n"; // usually "\n" on Linux/Mac or "\r\n" on Windows
  9.  
  10. List<String> condensedLines = new ArrayList();
  11. String[] lines = multiLineText.split(System.lineSeparator()); // alternative: use the regex
  12. for (String line : lines) {
  13. condensedLines.add(line.replaceAll("\\s+", " ")); // condense
  14. }
  15. String condensedPerLine = String.join(System.lineSeparator(), condensedLines);
  16. System.out.println(condensedPerLine);
  17. }
  18. }
  19.  
Success #stdin #stdout 0.1s 48516KB
stdin
Standard input is empty
stdout
 Hello World!
New line