Java String Multiply multiply(final String string, final int times)

Here you can find the source of multiply(final String string, final int times)

Description

multiply

License

Open Source License

Declaration

public static String multiply(final String string, final int times) 

Method Source Code

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

public class Main {
    public static String multiply(final char c, final int times) {
        if (times < 0) {
            throw new IllegalArgumentException();
        }/*from ww  w.ja va2  s .co  m*/
        if (times == 0) {
            return "";
        }

        final StringBuilder builder = new StringBuilder(times);

        for (int i = 0; i < times; i++) {
            builder.append(c);
        }

        return builder.toString();
    }

    public static String multiply(final String string, final int times) {
        if (times < 0) {
            throw new IllegalArgumentException();
        }
        if (times == 0) {
            return "";
        }

        final StringBuilder builder = new StringBuilder(string.length() * times);

        for (int i = 0; i < times; i++) {
            builder.append(string);
        }

        return builder.toString();
    }
}

Related

  1. multiply(final CharSequence str, final int factor)
  2. multiply(final String s, final Long factor)
  3. multiply(String original, int times)
  4. multiply(String self, int reps)
  5. multiply(String str11, String str22)
  6. multiplyString(String s, int count)