Java String Repeat repeat(String s, int times)

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

Description

Create a string by repeating a substring by specified times.

License

Open Source License

Parameter

Parameter Description
s a parameter
times a parameter

Declaration

public static String repeat(String s, int times) 

Method Source Code


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

import java.util.List;

public class Main {
    /**//w  w w .  j a v  a 2s  . c om
     * Create a string by repeating a substring by specified times.
     * @param s
     * @param times
     * @return
     */
    public static String repeat(String s, int times) {
        return repeat(s, times, null);
    }

    /**
     * Create a string by repeating a substring by specified times.
     * @param s
     * @param times
     * @param separator
     * @return
     */
    public static String repeat(String s, int times, String separator) {
        String[] arr = new String[times];
        for (int i = 0; i < times; i++) {
            arr[i] = s;
        }
        return join(arr, separator);
    }

    /**
     * Join a list of strings by comma.
     * @param strings
     * @return
     */
    public static String join(List<String> strings) {
        return join(strings.toArray(new String[0]), ", ");
    }

    /**
     * Join a list of strings by a separator.
     * @param strings
     * @param separator
     * @return
     */
    public static String join(List<String> strings, String separator) {
        return join(strings.toArray(new String[0]), separator);
    }

    /**
     * Join an array of strings by comma.
     * @param strings
     * @return
     */
    public static String join(String[] strings) {
        return join(strings, ", ");
    }

    /**
     * Join an array of strings by a separator.
     * @param strings
     * @param separator
     * @return
     */
    public static String join(String[] strings, String separator) {
        StringBuffer sb = new StringBuffer();
        if (strings.length > 0) {
            sb.append(strings[0]);
            for (int i = 1; i < strings.length; i++) {
                sb.append(separator);
                sb.append(strings[i]);
            }
        }
        return sb.toString();
    }
}

Related

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