Here you can find the source of alignLeft(String substring, int totalWidth, char fill)
Parameter | Description |
---|---|
substring | the substring to align |
totalWidth | the width of the returned string |
fill | the character to use for padding |
public static String alignLeft(String substring, int totalWidth, char fill)
//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(); } }