Formats the indent so that it uses as many tabs as possible - Android java.lang

Android examples for java.lang:String Pad

Description

Formats the indent so that it uses as many tabs as possible

Demo Code


public class Main{

    /** Formats the indent so that it uses as many tabs as possible */
    public static String formatIndent(String whiteSpace) {
        int tabs, spaces, newTabs;
        char chTemp;
        String strTemp, ret;/*from   ww  w  .  j av  a  2 s.  c  o m*/

        tabs = 0;
        spaces = 0;
        newTabs = 0;

        ret = "";

        for (int i = 0; i < whiteSpace.length(); i++) {
            chTemp = whiteSpace.charAt(i);
            strTemp = String.valueOf(chTemp);
            if (strTemp.equals(ASPACE))
                spaces++;
            else if (strTemp.equals(ATAB))
                tabs++;
        }
        if (spaces > 7) {
            newTabs = spaces / 8;
            spaces %= 8;
        }
        //Generate new indent
        for (int i = 0; i < tabs + newTabs; i++)
            ret += ATAB;
        for (int i = 0; i < spaces; i++)
            ret += ASPACE;

        return ret;
    }

}

Related Tutorials