string:centre/2
让字符串中间对齐
用法:
centre(String, Number) -> Centered
内部实现:
-spec centre(String, Number) -> Centered when String :: string(), Centered :: string(), Number :: non_neg_integer(). centre(String, Len) when is_integer(Len) -> centre(String, Len, $\s). -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 N = (Len-Slen) div 2, r_pad(l_pad(String, Len-(Slen+N), Char), N, Char); Slen =:= Len -> String end.
返回一个从字符串中间开始,扩充到指定长度 Number 的新字符串,不足则用空格填充,用法跟 string:centre(String, Number, $\s) 是一样
string:centre("123456789", 5)