[Erlang之旅 0008] string 常用函数

参考文档: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> 

[Erlang之旅 0008] string 常用函数,布布扣,bubuko.com

时间: 2024-12-14 22:40:06

[Erlang之旅 0008] string 常用函数的相关文章

C#string常用函数总结

补充:             1:在C语言里 char占1个字节 而在C#,Java里char占两个字节 数据库里char 中汉占两个字节 字母数字占一个字             2:string是c#中的类,String是.net Framework的类 string是关键字,也就是说string不能作为类.结构.枚举.字段.变量.方法.属性的名称,而String可以 一:  string是引用类型 但是当一个string类型的参数赋值给另一个参数时,只给了值,而不是地址,因为它保留在堆上

python学习笔记-day2-dict,tuple, string常用函数

上一篇说了Python的list的基本方法,这里接着说说dict,tuple. 一.tuple,元组 1.元组的定义 或: 2.使用切片访问元组里的元素 详细的可以参考list的切片用法 3.元组是不可修改,是不可变变量 4.访问元组里的元素 二.字典的使用 1.字典定义 2.字典的查询操作 3.添加字典的元素 有两种方法: (1) 使用如: infos['phone'] = 12345678977 #增加一个key (2) 使用 infos.setdefault('小金库', '2000w')

stl string常用函数

string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造函数和复制构造函数,如string s1:string s2="hello":都是正确的写法.当构造的string太长而无法表达时会抛出length_error异常 string类的字符操作: const char &operator[](int n)const; const ch

【转】string常用函数

原文地址:http://hi.baidu.com/baowup/blog/item/3a27465c86d71546faf2c066.html/cmtid/de1ef3f0de7554a0a40f52f7 string类的构造函数:string(constchar*s); //用c字符串s初始化string(int n,char c); //用n个字符c初始化此外,string类还支持默认构造函数和复制构造函数,如string s1:string s2="hello":都是正确的写法.

Java String常用函数

String 1. char charAt(int index) :取字符串中的某一个字符,其中的参数index指的是字符串中序数.字符串的序数从0开始到length()-1 . 2. int compareTo(String anotherString) :当前String对象与anotherString比较.相等关系返回0:不相等时,从两个字符串第0个字符开始比较,返回第一个不相等的字符差,另一种情况,较长字符串的前面部分恰巧是较短的字符串,返回它们的长度差. 4. String conca

Python string常用函数

2017-07-03 23:26:08 1..replace(self, old, new, count=-1) replace()函数将旧字符串替换为新字符串,最后一个参数count为可选项,表示替换最多count次(小于count). 注意这种替换返回替换后的字符串,源字符串是不改变的. s='ABCDEF' out=s.replace('EF','ef') print(s) print(out) 输出: ABCDEF ABCDef 2..find(self, sub, start=0, e

c++中string的常用函数说明

string可以说是是字符数组的升级版,使用更加啊方便,不容易出错.本文对string的常用函数进行简单介绍,做到会用即可. string中的常用函数分为四类,即赋值,添加,比较和删除. 一.赋值 1.str="adf" 2.str.assign("adf");str.assign(str1);//将括号中的字符串赋值给str: str.assign(str1,1,4);//将str1的1-4赋值给strs: str.assign(5,'a');//将五个a赋值给s

lambda函数,常用函数,内置函数(string,zip()map()filter())的用法

lambda函数胡使用#coding:utf-8g = lambda x,y:x*y/*必须亦g=*/print g(2,3)/*print必须有*/swtich函数使用 def jia(x,y):    return x+ydef jian(x,y):    return x-ydef cheng(x,y):    return x*ydef chu(x,y):    return x/yoperator = {"+":jia,"-":jian,"*&q

string 类常用函数[转]

string类的构造函数:string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初始化此外,string类还支持默认构造函数和复制构造函数,如string s1:string s2="hello":都是正确的写法.当构造的string太长而无法表达时会抛出length_error异常 string类的字符操作:const char &operator[](int n)const;const char &a