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

string:left/2

让字符串左对齐

用法:

left(String, Number) -> Left

内部实现:

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

left(String, Len) when is_integer(Len) -> left(String, Len, $\s).

-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 的新字符串,不足则用空格填充,用法跟 string:left(String, Number, $\s) 是一样

string:left("1234567890", 5).