> Erlang中文手册 > left/3 让字符串左对齐

string:left/3

让字符串左对齐

用法:

left(String, Number, Character) -> Left

内部实现:

-spec left(String, Number, Character) -> Left when
      String :: string(),
      Left :: string(),
      Number :: non_neg_integer(),
      Character :: char().

left(String, Len, Char) when is_integer(Char) ->
    Slen = length(String),
    if
	Slen > Len -> substr(String, 1, Len);
	Slen  l_pad(String, Len-Slen, Char);
	Slen =:= Len -> String
    end.

l_pad(String, Num, Char) -> String ++ chars(Char, Num).

返回一个从字符串左边开始,扩充到指定长度 Number 的新字符串,不足则用字符 Character 填充

string:left("1234567890", 20, $a).