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"

written by objects \\ tags: , , , ,


2 Responses to “How to fill a String with a character using Java?”

  1. Jon Says:

    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.

  2. Jon Says:

    Correction:

    String s = String.fill (count, character)

Leave a Reply

You must be logged in to post a comment.