Java StringBuilder repeat String
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { String sourceString = "demo2s.com"; int factor = 2; System.out.println(repeat(sourceString, factor)); }// www. ja v a 2 s.c o m /** * Returns sourceString concatenated together 'factor' times. * * @param sourceString The string to repeat * @param factor The number of times to repeat it. */ public static String repeat(String sourceString, int factor) { if (factor < 1) { return ""; } if (factor == 1) { return sourceString; } StringBuilder sb = new StringBuilder(factor * sourceString.length()); while (factor > 0) { sb.append(sourceString); factor--; } return sb.toString(); } }