Jan
29
|
The fill() method in the Arrays class is useful for creating string filled with a certain character.
// create a buffer of 9 characters char[] fill = new char[9]; // Fill the buffer with all '0's Arrays.fill(fill, '0'); // Create string using the buffer String zeroes = new String(fill); // zeroes string now contains "000000000"
Array ( ) 2 Responses to “How to fill a String with a character using Java?”
Leave a Reply
You must be logged in to post a comment.
October 16th, 2009 at 10:53 am
Ideally you want a prototype something like:
String s = String.fill (character)
I suppose you can take the above three lines and put it into a utility class method.
October 16th, 2009 at 10:55 am
Correction:
String s = String.fill (count, character)