Friday, May 14, 2021

Simple string padding

Just a quick way of centering a substring within a space passed string.  Nothings perfect, especially when odd numbers are concerned.

 proc pad { str max} {
    set len [string length $str]
    
    set tmp [string repeat " " $max]
    set dif [expr $max-$len]
    set fr [expr $dif/2]
    set to [expr $fr+$len]
    set str [string replace $tmp $fr $to $str]
    
    return $str
}


No comments: