prepend a string with filling character - Java java.lang

Java examples for java.lang:String Repeat

Description

prepend a string with filling character

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String source = "java2s.com";
        int length = 42;
        char fillChar = 'a';
        System.out.println(prepend(source, length, fillChar));
    }//  w  ww  . j  a v a 2 s .c om

    public static String prepend(String source, int length, char fillChar) {
        StringBuilder sb = new StringBuilder();
        while (sb.length() + source.length() < length) {
            sb.append(fillChar);
        }
        sb.append(source);

        return sb.toString();
    }

    public static int length(String source) {
        int result = 0;
        if (isNotEmpty(source)) {
            result = source.length();
        }
        return result;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isEmpty(String str) {
        return (str == null || str.length() == 0);
    }
}

Related Tutorials