Skip to content

Instantly share code, notes, and snippets.

@abdullah-alialdin
Forked from umidjons/java-repeat-string.md
Created August 14, 2018 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdullah-alialdin/6307af6b5fa52889fc348275061a2af2 to your computer and use it in GitHub Desktop.
Save abdullah-alialdin/6307af6b5fa52889fc348275061a2af2 to your computer and use it in GitHub Desktop.
Repeat string N times without loop in Java

Repeat string N times without loop in Java

package javaapplication1;
public class JavaApplication1 {
    /**
     * Repeat string <b>str</b> <b>times</b> time.
     * @param str string to repeat
     * @param times repeat str times time
     * @return generated string
     */
    public static String repeat(String str, int times) {
        return new String(new char[times]).replace("\0", str);
    }
    public static void main(String[] args) {
        System.out.println(repeat("*", 5));
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment