Repeat input multiplier times. - Java java.lang

Java examples for java.lang:String Repeat

Description

Repeat input multiplier times.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String input = "java2s.com";
        int multiplier = 42;
        System.out.println(repeat(input, multiplier));
    }/*from   ww  w  .  j ava  2 s  .c o m*/

    /**
     * Repeat input multiplier times. multiplier has to be greater than or
     * equal to 0. If the multiplier is <= 0, the function will return an
     * empty string.
     *
     * @param input
     * the input String
     * @param multiplier
     * how many times to repeat it
     * @return the resulting String
     */
    public static String repeat(final String input, final int multiplier) {
        if (multiplier <= 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < multiplier; i++) {
            sb.append(input);
        }
        return sb.toString();
    }
}

Related Tutorials