Java Tab Expand expandTabs(String string, int tabSize)

Here you can find the source of expandTabs(String string, int tabSize)

Description

Expands the given string's tabs according to the given tab size.

License

Open Source License

Parameter

Parameter Description
string the string
tabSize the tab size

Return

the expanded string

Declaration

private static StringBuffer expandTabs(String string, int tabSize) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM 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://from ww  w  . j  a v  a 2s .c  o  m
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Expands the given string's tabs according to the given tab size.
     *
     * @param string the string
     * @param tabSize the tab size
     * @return the expanded string
     * @since 3.1
     */
    private static StringBuffer expandTabs(String string, int tabSize) {
        StringBuffer expanded = new StringBuffer();
        for (int i = 0, n = string.length(), chars = 0; i < n; i++) {
            char ch = string.charAt(i);
            if (ch == '\t') {
                for (; chars < tabSize; chars++)
                    expanded.append(' ');
                chars = 0;
            } else {
                expanded.append(ch);
                chars++;
                if (chars >= tabSize)
                    chars = 0;
            }

        }
        return expanded;
    }
}

Related

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