首页 > 代码库 > [Erlang之旅 0008] string 常用函数
[Erlang之旅 0008] string 常用函数
参考文档:http://www.erlang.org/erldoc?q=string%3Bright&x=-967&y=-384
string类型,经常用到,所有练习一下:
3> string:len("fan"). %% 字符串长度34> string:len("你的").25> string:len("fan你的中国").76> string:equal("an","aa"). %%字符串比较false7> string:equal("an", "an"). true8> string:equal("an", ‘an‘).false9> string:concat("fan", "zhongguo"). %% 字符串添加"fanzhongguo"10> string:concat("fan", ‘中国 ‘).* 1: illegal atom10> string:concat("fan", ‘中国‘).* 1: illegal atom10> strign:concat("fan", "中国").** exception error: undefined function strign:concat/211> string:concat("fan", "中国").[102,97,110,20013,22269]12> string:concat("fan","china")."fanchina"13> string:chr("fan","a"). %% 返回字符串指定字符的位置 , 从左边查找** exception error: no function clause matching string:chr("fan","a") (string.erl, line 97)14> string:chr("fan",‘a‘).** exception error: no function clause matching string:chr("fan",a) (string.erl, line 97)15> string:chr("fan",$a). 216> string:chr("fanan",$a).217> strign:rchr("fanan",$a). ** exception error: undefined function strign:rchr/218> string:rchr("fanan", $a). %% 返回字符串指定字符串的位置, 从右边查找419> string:str("fanan","a").220> string:str("fanan", "an").221> string:rstr("fanan", "an").422> string:span("abcdef", "b").023> string:span("ab cdef", "b").024> string:substr("abcdef",2)."bcdef"25> string:substr("abcdef",2,6). %% 截取字符串,从指定位置或长度"bcdef"26> string:substr("abcdef",2,16)."bcdef"27> tokens("abcdef","a").** exception error: undefined shell command tokens/228> string:tokens("abcdefabc ","b").["a","cdefa","c "]29> string:tokens("abcdefabc", "bc"). %% 将字符串转化为list,按照指定字符或字符串["a","defa"]30> string:tokens("abcdefabc", "ab").["cdef","c"]31> string:join(["ab","c","def"],""). %% 将list,连接成字符串"abcdef"32> string:join(["ab","c","def"], "--")."ab--c--def"33> string:chars("a", 5).** exception error: no function clause matching string:chars("a",0,["a","a","a","a","a"]) (string.erl, line 256)34> string:chars($a, 5)."aaaaa"35> string:chars(‘a‘, 5).** exception error: no function clause matching string:chars(a,0,[a,a,a,a,a]) (string.erl, line 256)36> string:chars("ab",5).** exception error: no function clause matching string:chars("ab",0,["ab","ab","ab","ab","ab"]) (string.erl, line 256)37> string:chars($a,6,"bbcc"). %%给字符串固定长度,添加字符串"aaaaaabbcc"38> string:copies("abcdefabcdef", 5)."abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"39> string:copies("中国", 6).[20013,22269,20013,22269,20013,22269,20013,22269,20013, 22269,20013,22269]40> string:words("abcdef"). %% 字符串中以空格分开的字符的数量141> string:words("abcdef, ab").242> string:words("abcdef ab").243> string:words("abcdef ab, haa").344> string:words("abcdef ab, haa",a).** exception error: no function clause matching string:words("abcdef ab, haa",a) (string.erl, line 291)45> string:words("abcdef ab, haa","a").** exception error: no function clause matching string:words("abcdef ab, haa","a") (string.erl, line 291)46> string:words("abcdef ab, haa",$a). 247> string:words("abcdef ab haa", $a).248> string:words(" Hello old boy !", $o).449> string:words(" Hello old boy !", $l).350> string:sub_word("abcdef ab haa",2)."ab"51> string:sub_word("abcdef ab haa",6).[]52> string:sub_word("abcdef ab haa",3)."haa"53> string:sub_word("abcdef ab haa",0). ** exception error: no function clause matching string:s_word("abcdef ab haa",0,32,1,[]) (string.erl, line 321)54> string:sub_word("abcdef ab haa",1). "abcdef"55> string:sub_word("abcdef ab haa", 2, $a)."b h"56> string:sub_word("abcdef ab haa", 1, $a)."bcdef "57> string:strip(" abcdef "). %% 字符串进行裁剪"abcdef"58> string:strip(" abcdef ", left)."abcdef "59> stringstrip(" abcdef ", right).** exception error: undefined shell command stringstrip/260> strign:strip(" abcdef ", right).** exception error: undefined function strign:strip/261> string:strip(" abcdef ", right)." abcdef"62> string:strip(" abcdef ", both)."abcdef"63> string;strip("abcdeff", $f).* 1: syntax error before: ‘.‘63> string:strip("abcdeff", $f).** exception error: no function clause matching string:strip("abcdeff",102) (string.erl, line 340)64> string:strip("abcdeff", ‘f‘).** exception error: no function clause matching string:strip("abcdeff",f) (string.erl, line 340)65> string:strip("abcdeff", "f"). ** exception error: no function clause matching string:strip("abcdeff","f") (string.erl, line 340)66> string:strip("abcdeff",both, "f").** exception error: no function clause matching string:strip_left("abcdeff","f") (string.erl, line 356) in function string:strip/3 (string.erl, line 354)67> string:strip("abcdeff",both, $f). "abcde"68> string:left("abcdeff", 2)."ab"69> string:left("abcdeff", 2, $s)."ab"70> string:left("abcdeff", 20, $s)."abcdeffsssssssssssss"71> string:right("abcdeff", 2)."ff"72> string:right("abcdeff", 20, $s)."sssssssssssssabcdeff"73> string:centre("abcdeff", 2)."cd"74> string:centre("abcdeff", 5)."bcdef"75> string:centre("abcdeff", 1)."d"76> string:centre("abcdeffg", 1)."d"77> string:centre("abcdeffg", 2)."de"78> string:centre("abcdeffg ijklmndeffa", 2, $d)."ij"79> string:centre("abcdeffg ijklmndeffa", 2, $a)."ij"80> string:centre("abcdeffg ijklmndeffa", 2, $f)."ij"81> string:stu_string("abcdeffg", 1). ** exception error: undefined function string:stu_string/282> string:sub_string("abcdeffg", 2). "bcdeffg"83> string:sub_string("abcdeffg", 2,3). %% 获取子字符串,并且指定开始,和结束位置"bc"84> string:sub_string("abcdeffg", 2,9)."bcdeffg"85> string:to_float("abcdeffg").{error,no_float}86> string:to_float("20"). %% 字符串转化为浮点形{error,no_float}87> string:to_float("20.0").{20.0,[]}88> string:to_integer("20").{20,[]}89> string:to_integer("20.0").{20,".0"}90> string:to_integer("20.3").{20,".3"}91> string:to_lower("abcdefg"). %% 字符串大小写转化"abcdefg"92> string:to_upper("abcdef")."ABCDEF"93> string:to_upper("20")."20"94> string:to_upper($a). 6595> string:to_upper($A).6596>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。