Java String Whitespace Clean cleanWhitespaceAndIndentation(String s, String indent)

Here you can find the source of cleanWhitespaceAndIndentation(String s, String indent)

Description

clean Whitespace And Indentation

License

Open Source License

Declaration

public static String cleanWhitespaceAndIndentation(String s, String indent) 

Method Source Code

//package com.java2s;
/*******************************************************************************
* Copyright (c) 2012 - 2017 Signal Iduna Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
* 
* Contributors://  w w w.  ja  va2s .c o m
* itemis AG
*******************************************************************************/

public class Main {
    public static String cleanWhitespaceAndIndentation(String s, String indent) {
        String res = s.replaceAll("[\r\n]", "\n") //normalize line feeds
                .replaceAll("\n\\s*", "\n") //trim front
                .replaceAll("\\s*\n", "\n") //trim back
                .replaceAll("[\r\n]", "\n" + indent) //indent new lines
                .replaceAll("\n\\s*\n", "\n") //remove empty lines
                .replaceAll("}\n", "}\n\n"); //insert empty lines after '}'
        if (indent.length() >= 2) {
            res = res.replaceAll("\n\\s*}$", "\n" + indent.substring(2) + "}"); //un-indent if a line contains a '}'
        }

        return res;
    }
}

Related

  1. cleanWhitespace(String definition)
  2. cleanWhitespace(String in)
  3. cleanWhiteSpace(String s, boolean all)
  4. cleanWhitespace(String str)
  5. cleanWhitespace(String string)
  6. cleanWhitespaces(String string)