 |
BrandonNC Beginner
Joined: 03 Apr 2009 Posts: 10
|
Posted: Sun Dec 20, 2009 9:04 am
Center a string with %format? |
Is it possible to center a string somehow? For example, I want the total width of the string to be 60 characters, so if the string is:
"BrandonNC" then the output would be:
"__________________________BrandonNC_________________________" or something similar (with spaces of course). |
|
|
|
 |
charneus Wizard

Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Sun Dec 20, 2009 9:36 am |
Not that I know of, but I have a function that works just as well.
| Code: |
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
<func name="padcent" copy="yes">
<value>$Res=%repeat($Character,$Length)
$Res=%insert($String,$Res,(($Length-%len($String))/2)+1)
$Res=%left($Res,$Length)
#RETURN $Res</value>
<arglist>String, Length, Character</arglist>
</func>
</cmud>
|
The syntax is @padcent("string","Whole length of field","character you wish to fill in empty space")
So, in your example, you'd do:
#SAY {@padcenter("BrandonNC","60"," ")}
Charneus |
|
|
|
 |
gamma_ray Magician
Joined: 17 Apr 2005 Posts: 496
|
Posted: Sun Dec 20, 2009 10:36 am |
Here's another variation. This version can only pad with spaces, rather than with an arbitrary character.
| Code: |
$string="your string"
$totalLength=60
$pos = ($totalLength+%len($string))/2
#print {%format(&${pos}s,$string)} |
|
|
|
|
 |
BrandonNC Beginner
Joined: 03 Apr 2009 Posts: 10
|
Posted: Sun Dec 20, 2009 1:23 pm |
I finally got it with %format:
%format("&35s&-35s",%left(@Name, %len(@Name) / 2), %right(@Name, %len(@Name) / 2))
where @Name is "BrandonNC" or whatever. outputs a 70 character string with spaces on either end... (35 + 35) |
|
|
|
 |
|
|
|
|
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
|
|