Java String Repeat repeatString(String string, int count)

Here you can find the source of repeatString(String string, int count)

Description

repeatString

License

Open Source License

Parameter

Parameter Description
string a parameter
count a parameter

Return

String

Declaration

public static String repeatString(String string, int count) 

Method Source Code

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

import java.util.Arrays;
import java.util.Collection;

public class Main {
    /**/*from w  ww  .jav  a  2 s .  c  o  m*/
     * The empty string
     */
    public static final String EMPTY = "";

    /**
     * repeatString
     * 
     * @param string
     * @param count
     * @return String
     */
    public static String repeatString(String string, int count) {
        return (count > 0) ? join(string, new String[count + 1]) : EMPTY;
    }

    /**
     * Join one or more items placing the specified delimiter between each item
     * 
     * @param delimiter
     *            The delimiter to place between each item. This value may be
     *            null, in which case, the empty string will be the delimiter
     * @param items
     *            The items to join. This value may be null, in which case, the
     *            empty string will be returned
     * @return A single string that is the combination of the items and the
     *         delimiters
     */
    public static String join(String delimiter, Collection<String> items) {
        StringBuilder buffer = new StringBuilder();

        if (delimiter == null) {
            delimiter = "";
        }

        if (items != null) {
            for (String item : items) {
                if (item != null) {
                    buffer.append(item);
                }

                buffer.append(delimiter);
            }

            // trim off extra delimiter
            if (buffer.length() > 0) {
                buffer.setLength(buffer.length() - delimiter.length());
            }
        }

        return buffer.toString();
    }

    /**
     * Join one or more items placing the specified delimiter between each item
     * 
     * @param delimiter
     *            The delimiter to place between each item. This value may be
     *            null, in which case, the empty string will be the delimiter
     * @param items
     *            The items to join. This value may be null, in which case, the
     *            empty string will be returned
     * @return A single string that is the combination of the items and the
     *         delimiters
     */
    public static String join(String delimiter, String... items) {
        String result;

        if (items != null) {
            result = join(delimiter, Arrays.asList(items));
        } else {
            result = EMPTY;
        }

        return result;
    }
}

Related

  1. repeat(String string, int times)
  2. repeat(String val, int n)
  3. repeat(String value, int times)
  4. repeatChar(String c, int repeatCount)
  5. repeatImplodeString(String string, int length, String seperator)