开发者

Tcl add a symbol N time at the end of the string

How I can add N white-spaces at the end of the string, when fo开发者_如何学Gor example N = (25 - string_length)? Is there a command that does this procedure directly or I should use a loop?


Another way to tackle this is with the format command, if you know that you want a string of length 25 that is right padded with spaces then:

% format "|%-25s|" hello
|hello                    |

should do this. (The | is just there to delimit the result). It's also possible to use a variable to set the total width of the output:

% set width 25
25
% format "|%-*s|" $width hello
|hello                    |


There are several ways to do this:

With format

set padded [format "%-25s" $str]

(Also see Jackson's answer for this.)

With string repeat

set padded $str[string repeat " " [expr {25 - [string length $str]}]]

With append in a loop

for {set padded $str} {[string length $padded] < 25} {} {
    append padded " "
}

With binary format

set padded [binary format "A25" $str]

(Note that this is only safe for characters up to \u00FF.)


You can use string repeat, i.e.

% string repeat : 25
:::::::::::::::::::::::::
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜