[Solved] String formatting under a certain character [closed]


Since in the comment, the OP attempted to use String.format(), here is an approach to consider. Rather than trying to get the number to align right with the “%23d”, align the word and the count separately.

String.format("%-23s%2d:", getWord(), count);

The %-23d will format the getWord() in 23 spaces, left aligned, then the %2d will right align the “count”.

Example output:

Hello...................9:
Goodbye................42:
whatever...............17:

Note I just used the same .replace() approach as the OP for quickness sake.

See this attempt here at ideone.com

solved String formatting under a certain character [closed]