Create string with the specified number of characters example shows how to create string with n number of characters. The example also shows how to create fixed length string by repeating given character.
How to create a string with the specified number of characters in Java?
There are several ways using which you can create a string with the specified number of characters as given below.
1) Using StringBuilder/StringBuffer
You can use a loop and StringBuilder class to append the specified character number of times to create a fixed length string as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.javacodeexamples.stringexamples; public class StringWithSpecifiedCharsExample { //character to repeat static final String strCharacter = "*"; //required string length static final int stringLength = 10; public static void main(String[] args) { System.out.println(createString()); } public static String createString(){ StringBuilder sbString = new StringBuilder(stringLength); for(int i=0; i < stringLength; i++){ sbString.append(strCharacter); } return sbString.toString(); } } |
Output
1 |
********** |
Note: If you are using Java 1.4 or earlier versions, use the StringBuffer instead of the StringBuilder class.
2) Using the fill method of Arrays class
Create a char array and fill it with the character you want using fill
method of the Arrays class. Finally, convert the character array to a string using the String constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.javacodeexamples.stringexamples; import java.util.Arrays; public class StringWithSpecifiedCharsExample { //character to repeat static final char ch = '*'; //required string length static final int stringLength = 10; public static void main(String[] args) { System.out.println(createString()); } public static String createString(){ //create char array of specified length char[] charArray = new char[stringLength]; //fill all elements with the specified char Arrays.fill(charArray, ch); //create string from char array and return return new String(charArray); } } |
Output
1 |
********** |
3) Using the replace method of String class
Create a char array of fixed length and create a new String from it using the constructor to get the String of required length. Finally, replace all null characters (represented by ‘\0’) with the character of your choice.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.javacodeexamples.stringexamples; public class StringWithSpecifiedCharsExample { //character to repeat static final char ch = '*'; //required string length static final int stringLength = 10; public static void main(String[] args) { System.out.println(createString()); } public static String createString(){ //create new string from char array of required size String str = new String(new char[stringLength]); //replace all NUL chars '\0' with specified char str = str.replace('\0', ch); return str; } } |
Output
1 |
********** |
4) Using the repeat method of the Apache Commons library
If you are using Apache commons library, you can use repeat
method of StringUtils class to create a fixed length string with the specified character.
1 |
public static String repeat(char ch, int n) |
This method creates a new string by repeating specified character n times. You can also use overloaded repeat
method that accepts String instead of character.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.javacodeexamples.stringexamples; import org.apache.commons.lang3.StringUtils; public class StringWithSpecifiedCharsExample { //character to repeat static final char ch = '*'; //required string length static final int stringLength = 10; public static void main(String[] args) { String str = StringUtils.repeat(ch, stringLength); System.out.println(str); } } |
Output
1 |
********** |
This example is a part of the Java String tutorial with examples.
Please let me know your views in the comments section below.
thanks you and your support is so active for me
Hi,
Glad you liked my work. Keep on learning.