Java Tab Expand expandTabs(String s, int tabSize)

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

Description

expand Tabs

License

Open Source License

Declaration

public static String expandTabs(String s, int tabSize) 

Method Source Code

//package com.java2s;
/*/*from w  w  w . j  av a 2 s .  c o  m*/
 * Copyright (c) 2012-2016 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

public class Main {
    /** @since 4.6 */
    public static String expandTabs(String s, int tabSize) {
        if (s == null)
            return null;
        StringBuilder buf = new StringBuilder();
        int col = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            switch (c) {
            case '\n':
                col = 0;
                buf.append(c);
                break;
            case '\t':
                int n = tabSize - col % tabSize;
                col += n;
                buf.append(spaces(n));
                break;
            default:
                col++;
                buf.append(c);
                break;
            }
        }
        return buf.toString();
    }

    /** @since 4.6 */
    public static String spaces(int n) {
        return sequence(n, " ");
    }

    /** @since 4.6 */
    public static String sequence(int n, String s) {
        StringBuilder buf = new StringBuilder();
        for (int sp = 1; sp <= n; sp++)
            buf.append(s);
        return buf.toString();
    }
}

Related

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