Java String Align Left alignLeft(String substring, int totalWidth, char fill)

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

Description

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

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

Method Source Code

//package com.java2s;
/**//  ww w .j a 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 left, padded spaces on the right. 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 alignLeft(String substring, int totalWidth) {
        return alignLeft(substring, totalWidth, ' ');
    }

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

    /**
     * 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. alignLeft(CharSequence cs, int width, char c)
  2. alignLeft(String sLine, int iSize)
  3. alignLeft(String str, int length)
  4. alignLeft(String str, int size)
  5. alignLeft(String val, char pad, int width)