 |
kibln8 Beginner
Joined: 05 Sep 2005 Posts: 11
|
Posted: Mon Oct 08, 2007 10:43 pm
adding spaces to something |
hi I'm trying to capture stuff and format it... how can I make something that's 2 characters into 2 characters with 3 spaces, 3 characters to 3 characters with 2 spaces etc.?
|
|
|
 |
Fang Xianfu GURU

Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Mon Oct 08, 2007 10:48 pm |
Two ways - using %format or a construct like %concat(%repeat(" ",5-%len(%1)),%1) which will make a string 5 characters in width. Change the number 5 to whatever number you want.
|
|
|
 |
kibln8 Beginner
Joined: 05 Sep 2005 Posts: 11
|
Posted: Mon Oct 08, 2007 10:58 pm |
thanks
how would I make it add the spaces on to the other side? |
|
|
 |
Fang Xianfu GURU

Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Mon Oct 08, 2007 11:31 pm |
For right-align, move the concated space to the other side:
%concat(%1,%repeat(" ",5-%len(%1)))
For centre-align, it's a bit more complex. You'll want to have a repeat on both sides, with the length divided by 2. But if the length is odd, you'll need to add or remove spaces. If, say, %1 was 7 characters long, then %len(%1)/2 will be 3. If you were aiming for a 20-character string, you'd take away 3*2 from that to get 14, but then add 7 and end with 21 characters. Perhaps something like this will work:
%concat(%if(%mod(%len(%1),2)," "),%repeat(" ",10-%round(%float(%len(%1))/2),%1,%repeat(" ",10-%round(%float(%len(%1))/2))
This creates a centre-aligned 20-character string (or it should, anyway). It works by rounding up the length/2 - in the example above, this would give 4 rather than 3. 20-4*2 is 12, and then 7 is added to get 19. This is what the %if(%mod(%1,2)," ") is for - if %1's length is odd, this result will be 1 and an extra space will be added. If the length is even, the result is 0 and no space is added. It'd be simpler if there were ceiling and floor functions, but there's not, so it isn't. |
|
|
 |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|