Java String Whitespace Normalize normalizeIndentationFromWhitespace(String str, int tabSize, boolean insertSpaces)

Here you can find the source of normalizeIndentationFromWhitespace(String str, int tabSize, boolean insertSpaces)

Description

normalize Indentation From Whitespace

License

Open Source License

Declaration

private static String normalizeIndentationFromWhitespace(String str, int tabSize, boolean insertSpaces) 

Method Source Code

//package com.java2s;
/**/*from   ww  w .jav a2s . c  o  m*/
 *  Copyright (c) 2015-2017 Angelo ZERR.
 *  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:
 *  Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
 */

public class Main {
    private static String normalizeIndentationFromWhitespace(String str, int tabSize, boolean insertSpaces) {
        int spacesCnt = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == '\t') {
                spacesCnt += tabSize;
            } else {
                spacesCnt++;
            }
        }

        StringBuilder result = new StringBuilder();
        if (!insertSpaces) {
            long tabsCnt = Math.round(Math.floor(spacesCnt / tabSize));
            spacesCnt = spacesCnt % tabSize;
            for (int i = 0; i < tabsCnt; i++) {
                result.append('\t');
            }
        }

        for (int i = 0; i < spacesCnt; i++) {
            result.append(' ');
        }

        return result.toString();
    }
}

Related

  1. normaliseWhitespace(String string)
  2. normaliseWhitespace(String string)
  3. normaliseWhitespace(String string)
  4. normalizeWhitespace(final String str)
  5. normalizeWhitespace(String dirtyString)
  6. normalizeWhiteSpace(String input)
  7. normalizeWhiteSpace(String input)