[Erlang脚印 0008] string 常用函数

来源:互联网 时间:1970-01-01

参考文档:http://www.erlang.org/erldoc?q=string%3Bright&x=-967&y=-384

string类型,经常用到,所有练习一下:

3> string:len("fan"). %% 字符串长度

3

4> string:len("你的").

2

5> string:len("fan你的中国").

7

6> string:equal("an","aa"). %%字符串比较

false

7> string:equal("an", "an").

true

8> string:equal("an", 'an').

false

9> string:concat("fan", "zhongguo"). %% 字符串添加

"fanzhongguo"

10> string:concat("fan", '中国 ').

* 1: illegal atom

10> string:concat("fan", '中国').

* 1: illegal atom

10> strign:concat("fan", "中国").

** exception error: undefined function strign:concat/2

11> 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).

2

16> string:chr("fanan",$a).

2

17> strign:rchr("fanan",$a).

** exception error: undefined function strign:rchr/2

18> string:rchr("fanan", $a). %% 返回字符串指定字符串的位置, 从右边查找

4

19> string:str("fanan","a").

2

20> string:str("fanan", "an").

2

21> string:rstr("fanan", "an").

4

22> string:span("abcdef", "b").

0

23> string:span("ab cdef", "b").

0

24> 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/2

28> 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"). %% 字符串中以空格分开的字符的数量

1

41> string:words("abcdef, ab").

2

42> string:words("abcdef ab").

2

43> string:words("abcdef ab, haa").

3

44> 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).

2

47> string:words("abcdef ab haa", $a).

2

48> string:words(" Hello old boy !", $o).

4

49> string:words(" Hello old boy !", $l).

3

50> 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/2

60> strign:strip(" abcdef ", right).

** exception error: undefined function strign:strip/2

61> 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/2

82> 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).

65

95> string:to_upper($A).

65

96>


相关阅读:
Top