Requirement:
Repeat string with a separator in Java. For example,“Java” when repeated three times with a hyphen to produce “Java-Java-Java”
JAR Files Download:
Download a copy of commons-lang3-3.1.jar and keep it in your class path.Java Code Example:
The program that you can use to repeat strings in Java with a separator is provided below:import org.apache.commons.lang3.StringUtils; // to repeat the string
public class repeatStringSeparator {
public static void main (String[] args) {
String myInput="Java";
String separator="-";
int number_of_times=3;
String newRepeatedString=StringUtils.repeat(myInput,separator,number_of_times);
//we repeated the string 3 times
System.out.println("Input String: " + myInput);
System.out.println("Repeated String: " + newRepeatedString);
}
}
Example Output:
C:\Java\commons-lang3-3.1>javac -classpath .;commons-lang3-3.1.jar repeatStringS eparator.java C:\Java\commons-lang3-3.1>java -classpath .;commons-lang3-3.1.jar repeatStringSe parator Input String: Java Repeated String: Java-Java-Java