524.Left Pad

2017-07-22  本文已影响0人  博瑜
public class StringUtils {
/**
 * @param originalStr the string we want to append to with spaces
 * @param size the target length of the string
 * @return a string
 */
static public String leftPad(String originalStr, int size) {
    // Write your code here
     return leftPad( originalStr,  size, ' ');
}

/**
 * @param originalStr the string we want to append to
 * @param size the target length of the string
 * @param padChar the character to pad to the left side of the string
 * @return a string
 */
static public String leftPad(String originalStr, int size, char padChar) {
    // Write your code here
    int length = originalStr.length();
    StringBuilder sb = new StringBuilder();
    int addLength = size - length;
    for (int i = 0; i < addLength; i++) {
        sb.append(padChar);
    }
    sb.append(originalStr);
    return sb.toString();
}
}
上一篇 下一篇

猜你喜欢

热点阅读