Java Tab Expand expandTabs(String text, int tabWidth)

Here you can find the source of expandTabs(String text, int tabWidth)

Description

Same as expandTabs(text, tabWidth, 0) .

License

Apache License

Declaration

public static String expandTabs(String text, int tabWidth) 

Method Source Code

//package com.java2s;
/*//ww w .ja v a2s  . c om
 * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Same as {@code expandTabs(text, tabWidth, 0)}.
     * @see #expandTabs(String, int, int) 
     */
    public static String expandTabs(String text, int tabWidth) {
        return expandTabs(text, tabWidth, 0);
    }

    /**
     * Replaces all occurances of character tab with spaces.
     * @param tabWidth the distance of tab stops.
     * @param startCol the index of the column in which the first character of
     *     the string is from the left edge of the page. The index of the first
     *     column is 0.
     * @return String The string after the replacements.
     */
    public static String expandTabs(String text, int tabWidth, int startCol) {
        int e = text.indexOf('\t');
        if (e == -1) {
            return text;
        }
        int b = 0;
        int tln = text.length();
        StringBuffer buf = new StringBuffer(tln + 16);
        do {
            buf.append(text.substring(b, e));
            int col = buf.length() + startCol;
            for (int i = tabWidth * (1 + col / tabWidth) - col; i > 0; i--) {
                buf.append(' ');
            }
            b = e + 1;
            e = text.indexOf('\t', b);
        } while (e != -1);
        buf.append(text.substring(b));
        return buf.toString();
    }
}

Related

  1. expandTabs(String inputString, int numberOfSpaces)
  2. expandTabs(String s, int tabSize)
  3. expandTabs(String str)
  4. expandTabs(String string, int tabSize)
  5. expandTabs(String text, int tabSize)