We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Basically this expression (%-" + i + "s","#") means:

Apply this format (%-" + i + "s") to the parametrized String #.
%: means a formatting expression will follow.
-: means you want to padd to the rigth.( + or none means that you want to padd to the left).
i: means the length of the string that you expect. The length of the missing characters will filled with blank spaces.
s: denotes the type of the object, in this case strings.

For each iteration I get:

'#' where i=1. No padding is added because you expect a string of length 1.
'# ' where i=2.
'# ' where i=3.
.... until '# ' where i = 6.

For each iteration I replace blank spaces with # (.replace(' ','#');). So finally I get:

'#'
'##'
'###'
'####'
'#####'
'######'

For each iteration also I apply a final formatting:

String.format("%" + (n) + "s",padded).

Basically I am saying I expect in every iteration a string with a length of 6 characteres and every missing position will filled with blank spaces to the left. So I get:

## Staircase

You are viewing a single comment's thread. Return to all comments →

Sure.

Basically this expression (%-" + i + "s","#") means:

For each iteration I get:

For each iteration I replace blank spaces with # (.replace(' ','#');). So finally I get:

For each iteration also I apply a final formatting:

Basically I am saying I expect in every iteration a string with a length of 6 characteres and every missing position will filled with blank spaces to the left. So I get:

And that's all.

wow!. thats pretty neat solution. thanks for the great explanation, much appreciated.

welcome