class TStringUtil extends StringUtils {

    public static String fillTabWithSpace(String str) {
        if (str == null) {
            return null;
        }

        str = str.trim();
        int sz = str.length();
        StringBuilder buffer = new StringBuilder(sz);

        int index = 0, index0 = -1, index1 = -1;
        for (int i = 0; i < sz; i++) {
            char c = str.charAt(i);
            if (!Character.isWhitespace(c)) {
                if (index0 != -1) {
                    // if (!(index0 == index1 && str.charAt(i - 1) == ' ')) {
                    if (index0 != index1 || str.charAt(i - 1) != ' ') {
                        buffer.append(str.substring(index, index0)).append(" ");
                        index = index1 + 1;
                    }
                }
                index0 = index1 = -1;
            } else {
                if (index0 == -1) {
                    index0 = index1 = i; // 第一个空白
                } else {
                    index1 = i;
                }
            }
        }

        buffer.append(str.substring(index));
        return buffer.toString();
    }

}