Java String Format format(final String s, final int width, final int intend)

Here you can find the source of format(final String s, final int width, final int intend)

Description

format

License

Open Source License

Declaration

public static String format(final String s, final int width, final int intend) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    public static String format(final String s, final int width, final int intend) {
        final String ws = ws(intend);
        if (s.length() < width - intend)
            return ws + s;
        final StringBuilder buf = new StringBuilder();
        int pos = 0;
        while (pos + width - intend < s.length()) {
            buf.append(ws).append(s.substring(pos, pos + width - intend)).append("\n");
            pos += width - intend;/*from  w w  w .j a  va2  s.  c om*/
            int indexOfS = s.indexOf(' ', pos);
            while (indexOfS == pos) {
                pos++;
                indexOfS = s.indexOf(' ', pos);
            }
        }
        buf.append(ws).append(s.substring(pos));
        return buf.toString();
    }

    public static String ws(final int length) {
        return repeat(' ', length);
    }

    public static String repeat(final char c, final int count) {
        if (count <= 0)
            return "";
        final char[] a = new char[count];
        Arrays.fill(a, c);
        return new String(a);
    }
}

Related

  1. createFormatter()
  2. format(final String message, final Object... args)
  3. format(String longMessage, int charPerLine, int paddingLeft, boolean padFirstLine)
  4. format(String name, char separator)
  5. format(String name, char separator, String prefix, boolean includePrefix, boolean includeLeadingSeparator)
  6. format(String s)