Repeat a char in Java
Description
The following code shows how to repeat a char.
Example
//from www .j a va 2 s .c o m
public class Main {
public static String repeat(char ch, int count) {
StringBuffer buffer = null;
buffer = new StringBuffer(count);
for (int i = 1; i <= count; i++) {
buffer.append(ch);
}
return (buffer.toString());
}
public static void main(String[] argv){
System.out.println(repeat('a',4));
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »