Java String Align Right alignRight(String substring, int totalWidth, char fill)

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

Description

Returns a string of the specified length where the substring is located to the right, padded with a character on the left.

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 alignRight(String substring, int totalWidth, char fill) 

Method Source Code

//package com.java2s;
/**//from   w  ww  .ja v  a  2s.co m
 *
 * 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
     * to the right, padded with spaces on the left. 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 alignRight(String substring, int totalWidth) {
        return alignRight(substring, totalWidth, ' ');
    }

    /**
     * Returns a string of the specified length where the substring is located
     * to the right, padded with a character on the left. 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 alignRight(String substring, int totalWidth, char fill) {
        if (substring.length() > totalWidth) {
            return substring.substring(0, totalWidth);
        } else {
            return repeat("" + fill, totalWidth - substring.length()) + substring;
        }
    }

    /**
     * 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. alignRight(final long number, final int length)
  2. alignRight(final Object str, final int size)
  3. alignRight(String data)
  4. alignRight(String str, int length, boolean isEllipsis)
  5. alignRight(String str, int size, char padChar)
  6. alignRight(String text, int length)
  7. alignRight(String val, char pad, int width)