Java String Repeat repeat(String s, int times)

Here you can find the source of repeat(String s, int times)

Description

Generate a string that repeats/replicates a string a specified number of times.

License

Open Source License

Parameter

Parameter Description
s is the string to repeat.
times is the number of times to repeat the string.

Return

a concatenation of times instances of s.

Declaration

public static String repeat(String s, int times) 

Method Source Code

//package com.java2s;

import java.util.List;

public class Main {
    /**/*from  w w  w  . j av a 2  s .  co m*/
     * Generate a string that repeats/replicates a string a specified number of
     * times.
     * 
     * @param s
     *          is the string to repeat.
     * @param times
     *          is the number of times to repeat the string.
     * @return a concatenation of times instances of s.
     */
    public static String repeat(String s, int times) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < times; ++i) {
            sb.append(s);
        }
        return sb.toString();
    }

    public static String toString(List<?> arr) {
        return toString(arr, true);
    }

    public static String toString(List<?> arr, boolean square) {
        return toString(arr.toArray(), square);
    }

    public static String toString(Object[] arr) {
        return toString(arr, true);
    }

    public static String toString(Object[] arr, boolean square) {
        if (arr == null)
            return "null";
        StringBuffer sb = new StringBuffer();
        if (square) {
            sb.append("[");
        } else {
            sb.append("(");
        }
        for (int i = 0; i < arr.length; ++i) {//Object o : arr ) {
            if (i > 0)
                sb.append(",");
            if (arr[i] == null) {
                sb.append("null");
            } else {
                sb.append(arr[i].toString());
            }
        }
        if (square) {
            sb.append("]");
        } else {
            sb.append(")");
        }

        return sb.toString();
    }
}

Related

  1. deleteRepeatStr(String repeatStr, String separator)
  2. repeat(String in, int count)
  3. repeat(String item, int count)
  4. repeat(String pattern, int count)
  5. repeat(String s, int cnt)
  6. repeat(String s, int times)
  7. repeat(String s, int times)
  8. repeat(String str, int count)
  9. repeat(String str, int repeat)