Here you can find the source of expandTabs(String s, int tabSize)
public static String expandTabs(String s, int tabSize)
//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(); } }