Tim M wrote:
> I keep looking for ways to pad a String or any Collection - and I don't
> see any methods for this?
Me too.
> The best I could come up with was:
>
> padded := aCollection , (aCollection class new: desiredSize -
> aCollection size - 1 withAll: paddingChar)
>
> But there must be a more elegent way to do this?
How about:
padded := aCollection paddedTo: desiredSize.
:-)
At least given Space as the paddingChar.
Seems I added the following extension to String some time ago:
!String methodsFor!
paddedTo: anInteger
^self , (self species new: (anInteger - self size max: 0)
withAll: Character space)! !
I also have a similar #rightJustify: that pads on the left with spaces.
BTW, I think the "-1" in your example must be a bug.
Another good reason to make the code into its own method, which can then
be easily tested. I found I also have:
!StringExtensionsTest methodsFor!
testPaddedTo
self assert: ('abc' paddedTo: 4) = 'abc '.
self assert: ('abc' paddedTo: 3) = 'abc'.
self assert: ('abc' paddedTo: 2) = 'abc'.
self assert: ('abc' paddedTo: 0) = 'abc'.
self assert: ('' paddedTo: 0) = ''.
self assert: ('' paddedTo: 2) = ' '! !
--
Bill Dargel
[hidden email]
Shoshana Technologies
100 West Joy Road, Ann Arbor, MI 48105 USA