string:centre/3
让字符串中间对齐
用法:
centre(String, Number, Character) -> Centered
内部实现:
-spec centre(String, Number, Character) -> Centered when String :: string(), Centered :: string(), Number :: non_neg_integer(), Character :: char(). centre(String, 0, Char) when is_list(String), is_integer(Char) -> []; % Strange cases to centre string centre(String, Len, Char) when is_integer(Char) -> Slen = length(String), if Slen > Len -> substr(String, (Slen-Len) div 2 + 1, Len); Slen < Len -> N = (Len-Slen) div 2, r_pad(l_pad(String, Len-(Slen+N), Char), N, Char); Slen =:= Len -> String end.
返回一个从字符串中间开始,扩充到指定长度 Number 的新字符串,不足则用字符 Character 填充
string:centre("123456789", 20, $a)