> Erlang中文手册 > right/2 让字符串右对齐

string:right/2

让字符串右对齐

用法:

right(String, Number) -> Right

内部实现:

-spec right(String, Number) -> Right when
      String :: string(),
      Right :: string(),
      Number :: non_neg_integer().

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

-spec right(String, Number, Character) -> Right when
      String :: string(),
      Right :: string(),
      Number :: non_neg_integer(),
      Character :: char().

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

r_pad(String, Num, Char) -> chars(Char, Num, String).

返回一个从字符串右边开始,扩充到指定长度 Number 的新字符串,不足则用空格填充,用法跟 string:right(String, Number, $\s) 是一样

string:right("1234567890", 5).