Java String Format format(String longMessage, int charPerLine, int paddingLeft, boolean padFirstLine)

Here you can find the source of format(String longMessage, int charPerLine, int paddingLeft, boolean padFirstLine)

Description

Format long message in multiple line in such a way that each line does not contain more charPerLine .

License

Open Source License

Parameter

Parameter Description
longMessage a long message to display over command line
charPerLine maximum number of character per line
paddingLeft number of spaces prepended to each line
padFirstLine if true first line is also prepended

Return

multi-line formatted message

Declaration

public static String format(String longMessage, int charPerLine, int paddingLeft, boolean padFirstLine) 

Method Source Code


//package com.java2s;
/*//from  ww  w.  ja va 2s  .c  om
 * Scaffold Hunter
 * Copyright (C) 2006-2008 PG504
 * Copyright (C) 2010-2011 PG552
 * Copyright (C) 2012-2014 LS11
 * See the file README.txt in the root directory of the Scaffold Hunter
 * source tree for details.
 *
 * Scaffold Hunter is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Scaffold Hunter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Arrays;

public class Main {
    private static final String NEW_LINE = "\n";

    /**
     * Format long message in multiple line in such a way that each line does
     * not contain more {@code charPerLine}. Each line contains number of
     * characters equal to or less than {@code charPerLine}. {@code paddingLeft}
     * indicates the number of spaces prepended to each line. And final
     * {@code padFirstLine} tells whether the first line is to be padded
     * 
     * @param longMessage
     *            a long message to display over command line
     * @param charPerLine
     *            maximum number of character per line
     * @param paddingLeft
     *            number of spaces prepended to each line
     * @param padFirstLine
     *            if true first line is also prepended
     * @return multi-line formatted message
     */
    public static String format(String longMessage, int charPerLine, int paddingLeft, boolean padFirstLine) {
        char[] padChar = new char[paddingLeft];
        longMessage = longMessage.replaceAll("<[/]*[a-zA-Z]+>", "");
        Arrays.fill(padChar, ' ');

        StringBuilder builder = new StringBuilder();

        if (padFirstLine) {
            builder.append(padChar);
        }

        String[] parts = longMessage.split(" ");
        int processed = 0;
        for (String message : parts) {
            if (processed >= charPerLine) {
                builder.append(NEW_LINE).append(padChar).append(message).append(' ');
                processed = message.length() + 1;
            } else {
                builder.append(message).append(' ');
                processed += message.length() + 1;
            }
        }
        return builder.toString();
    }
}

Related

  1. createFormatter()
  2. format(final String message, final Object... args)
  3. format(final String s, final int width, final int intend)
  4. format(String name, char separator)
  5. format(String name, char separator, String prefix, boolean includePrefix, boolean includeLeadingSeparator)
  6. format(String s)
  7. format(String source, String placeholder, Object... objs)