string:sub_word/2
获取指定位置的单词
用法:
sub_word(String, Number) -> Word
内部实现:
-spec sub_word(String, Number) -> Word when String :: string(), Word :: string(), Number :: integer(). sub_word(String, Index) -> sub_word(String, Index, $\s). -spec sub_word(String, Number, Character) -> Word when String :: string(), Word :: string(), Number :: integer(), Character :: char(). sub_word(String, Index, Char) when is_integer(Index), is_integer(Char) -> case words(String, Char) of Num when Num < Index -> []; _Num -> s_word(strip(String, left, Char), Index, Char, 1, []) end. s_word([], _, _, _,Res) -> reverse(Res); s_word([Char|_],Index,Char,Index,Res) -> reverse(Res); s_word([H|T],Index,Char,Index,Res) -> s_word(T,Index,Char,Index,[H|Res]); s_word([Char|T],Stop,Char,Index,Res) when Index < Stop -> s_word(strip(T,left,Char),Stop,Char,Index+1,Res); s_word([_|T],Stop,Char,Index,Res) when Index < Stop -> s_word(T,Stop,Char,Index,Res).
获取字符串 String 里的第 Number 个单词,默认使用空格来做单词的分隔符。
string:sub_word("a b c d e f g", 4).