Home Segments Index Top Previous Next

490: Mainline

Evidently, to produce a string of a prescribed length from a given string, you determine how many padding characters you need, you create a new string filled with that number of padding characters, and you concatenate the new string with the given string. All these steps are expressed in the following definition of the formatWithWidth: method:

String method definition • instance 
formatWithWidth: w 
                       *-- Number of padding spaces required 
                       | 
                       v 
                 -------------     
  ^ (String new: w - self size) atAllPut: $  , self printString 
    ---------------------------------------- 
                       ^ 
                       | 
                       *-- String filled with padding spaces 
    ----------------------------------------------------------- 
                         ^ 
                         | 
                         *-- Padded string