Erlang Central

Difference between revisions of "Aligning Strings"

From ErlangCentral Wiki

m
m (Reverted edits by Kaiserpanda (Talk); changed back to last version by 213.171.204.166)
Line 20: Line 20:
  
 
[[Category:CookBook]][[Category:StringRecipes]]
 
[[Category:CookBook]][[Category:StringRecipes]]
 
 
 
[http://www.3wcasinos.com/roulette-tips/index.html roulette tips]
 
[http://www.casinos-new.com/blackjack.html Online casino - Blackjack online]
 
[http://www.magical-casino.com/casino_rules.html Casino rules.]
 
[http://www.casinos-go.com/online-casino-tips/free-online-casino-slot.html free online casino slot]
 
[http://www.slots-wiki.com/index.php/slots_hints slots hints]
 
[http://www.casinos-new.com/internet-casino.html Online casino - Internet Casinos]
 
[http://www.online-casino-wiki.com/index.php/online_casino_tips online casino tips]
 
[http://www.gambling-online-theory.com/casinos/fun-casinos.html fun casinos]
 
[http://www.gambling-online-theory.com/casinos-portal/internet-casinos-sites.html internet casinos sites]
 
[http://www.gambling-online-theory.com/casinos/fun-casinos.html fun casinos]
 

Revision as of 08:50, 4 December 2006

Problem

You want to align a string, left, right, or center. You might use this when generating reports, for example.

Solution

The easiest solution is to use the string module, which solves the left and right padding issues.

1> io:fwrite("|~s|~s|~n", [ string:left("Title",20), string:right("Price",10) ]).
|Title               |     Price|
ok
2> io:fwrite("|~s|~n", [ string:centre("Fabulous", 25) ]).
|        Fabulous         |
ok

left and right both default to using '$ ' (the space character) as the padding character; you may override this by providing the character in the third argument. The left and right functions also truncate the string if it's longer than the specified field width, but centre generates an error in that case.