Java String Repeat repeat(String string, int n)

Here you can find the source of repeat(String string, int n)

Description

Like a monkey with a miniature symbol.

License

Open Source License

Parameter

Parameter Description
string a parameter
n a parameter

Return

stringstringstring...

Declaration

public static String repeat(String string, int n) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    /**/*from www.  j  a  v a 2  s. c o m*/
     * Repeat a character
     * 
     * @param c
     * @param n
     * @return e.g. '-',5 creates "-----"
     */
    public static String repeat(char c, int n) {
        char[] chars = new char[n];
        Arrays.fill(chars, c);
        return new String(chars);
    }

    /**
     * Like a monkey with a miniature symbol. The joy of repetition really is in
     * you. ?? Does this deserve to be a utility method?
     * 
     * @param string
     * @param n
     * @return stringstringstring...
     */
    public static String repeat(String string, int n) {
        StringBuilder sb = new StringBuilder(string.length() * n);
        for (int i = 0; i < n; i++) {
            sb.append(string);
        }
        return sb.toString();
    }
}

Related

  1. repeat(String s, int times)
  2. repeat(String s, int times)
  3. repeat(String str, int count)
  4. repeat(String str, int repeat)
  5. repeat(String str, int repeat)
  6. repeat(String string, int number)
  7. repeat(String string, int times)
  8. repeat(String string, int times)
  9. repeat(String val, int n)