Java String Align Center alignCenter(String substring, int totalWidth, char fill)

Here you can find the source of alignCenter(String substring, int totalWidth, char fill)

Description

Returns a string of the specified length where the substring is located in the middle, padded with a character on both sides.

License

Open Source License

Parameter

Parameter Description
substring the substring to align
totalWidth the width of the returned string
fill the character to use for padding

Return

the padded string

Declaration

public static String alignCenter(String substring, int totalWidth, char fill) 

Method Source Code

//package com.java2s;
/**/*from  ww w .java2s  . com*/
 *
 * Copyright (c) 2006-2017, Speedment, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); You may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

public class Main {
    /**
     * Returns a string of the specified length where the substring is located
     * in the middle, padded with spaces on both sides. If uneven, an extra
     * space will be added to the right side. If the specified 
     * {@code totalWidth} is less than the length of the substring, the
     * substring is returned but with the overflowing characters removed.
     * 
     * @param substring   the substring to align
     * @param totalWidth  the width of the returned string
     * @return            the padded string
     */
    public static String alignCenter(String substring, int totalWidth) {
        return alignCenter(substring, totalWidth, ' ');
    }

    /**
     * Returns a string of the specified length where the substring is located
     * in the middle, padded with a character on both sides. If uneven, an extra
     * space will be added to the right side. If the specified 
     * {@code totalWidth} is less than the length of the substring, the
     * substring is returned but with the overflowing characters removed.
     * 
     * @param substring   the substring to align
     * @param totalWidth  the width of the returned string
     * @param fill        the character to use for padding
     * @return            the padded string
     */
    public static String alignCenter(String substring, int totalWidth, char fill) {
        if (substring.length() > totalWidth) {
            return substring.substring(0, totalWidth);
        } else {
            final double padding = (totalWidth - substring.length()) / 2d;
            final int left = (int) Math.floor(padding);
            final int right = (int) Math.ceil(padding);
            return repeat("" + fill, left) + substring + repeat("" + fill, right);
        }
    }

    /**
     * Repeats the specified substring a number of times.
     *
     * @param str    the string to repeat
     * @param count  the number of times to repeat it
     * @return       the new string
     */
    public static String repeat(String str, int count) {
        final StringBuilder result = new StringBuilder(str.length() * count);

        for (int i = 0; i < count; i++) {
            result.append(str);
        }

        return result.toString();
    }
}

Related

  1. alignCenter(String sLine, int iSize)
  2. alignCenter(String str, int length)