Thursday, March 19, 2009

How to fill a String with a character using Java?

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"

No comments: