Here you can find the source of repeat(String str, int repeat)
public static String repeat(String str, int repeat)
//package com.java2s; /**//from www . ja va2 s . c o m * Copyright 2013 ?????? * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { public static final String EMPTY = ""; public static String repeat(String str, int repeat) { if (str == null) { return null; } if (repeat < 1) { return EMPTY; } int inputLen = str.length(); if (inputLen == 0 || repeat == 1) { return str; } int outputLen = inputLen * repeat; if (inputLen == 1) { char ch = str.charAt(0); char[] output = new char[outputLen]; for (int i = 0; i < outputLen; i++) { output[i] = ch; } return new String(output); } else { StringBuilder output = new StringBuilder((int) Math.min( (outputLen * 110L) / 100, Integer.MAX_VALUE)); for (int i = 0; i < repeat; i++) { output.append(str); } return output.toString(); } } }