• erlang 中的 string 应用


    string 的一些功能操作,这些可能和wxTextCtrl 控件配合使用的比较多

    (0)目的:["a","b","c","d"] -> "abcd"
    L1 = ["a","b","c","d"],
    binary_to_list(list_to_binary(L1)).
    "abcd"
    (1)求字符串的长度
    string:len("abcdefabc"). -> 9
    判断两个字符串是否相等
    string:equal("abc","abc").
    true
    (2)两个字符串组合为一个字符串
    string:concat("abc","def").
    "abcdef"
    (3)求第一个或者最后一个某字符在字符串中的顺序,不存在为0,
    以1为起始坐标
    string:chr("abdcdef",$d).
    3
    string:rchr("abdcdef",$d).
    5
    (4)求某个子字符串在字符串中出现的第一或者最后位置
    string:str("hehe haha haha","haha").
    6
    string:rstr("hehe haha haha","haha").
    11
    (5)截取字符串 以及 从起始到的若干个字符串
    string:substr("Hello World",4).
    "lo World"
    string:substr("Hello World",4,5).
    "lo Wo"

    (6)分割字符串,但是用于分割的源字符串中的字符就不出现了
    string:tokens("asdhfgjjdttfg","df").
    ["as","h","gjj","tt","g"]
    (7)用特定字符连接字符串
    string:join(["aaa","bbb","ccc"],"@").
    "aaa@bbb@ccc"
    (8) 若干字符连接成一个字符串
    string:chars($a,5).
    "aaaaa"
    string:chars($d, 10, "ee").
    "ddddddddddee"
    (9) 若干字符串连接成一个字符串,
    string:copies("as",5).
    "asasasasas"
    若干个字符串连接成一个字符串,并以某个字符串结尾
    string:words("aaa bbb ccc").
    3
    (10)字符串中有几个隔开的字(word)
    string:words("abcbchdbjfb",$b).
    4
    (11) 用字符隔开后取第几个字符串
    string:sub_word("abcbchdbjfb",3,$b).
    "chd"

    (12)去掉字符串某方向的某一些字符,如左,右,两边,默认去掉的是空格
    默认去掉的是两边的
    string:strip(" aaa ").
    "aaa"
    string:strip("...aaa..",both,$.).
    "aaa"
    string:strip("...aaa..",left,$.).
    "aaa.."
    string:strip("...aaa..",right,$.).
    "...aaa"

    (13)截取前面几个字符
    截取前面几个字符,如果不足,以某个字符补齐,
    还可以是右边或者中间
    string:centre("HelloHello", 5).
    "lloHe"
    string:centre("HelloHello", 20).
    " HelloHello "
    string:centre("HelloHello", 20, $.).
    ".....HelloHello....."
    (14)
    所有字符转为大写或者所有字符转为小写
    string:to_lower("AAA123").
    "aaa123"
    string:to_upper("bbb123").
    "BBB123"



    (15)里面有string:span 和 string:rpan 是匹配的还没有看明白
    (16)string转为 integer
    list_to_integer("11111").
    11111
    list_to_integer("11111aaa").
    ** exception error: bad argument
    in function list_to_integer/1
    called as list_to_integer("11111aaa")
    string:to_integer("1111aaa").
    {1111,"aaa"}
    {I1,Is} = string:to_integer("33+22"),
    {I2,[]} = string:to_integer(Is),
    I1-I2.
    11
    (17)string转为 float
    {F1,Fs} = string:to_float("1.0-1.0e-1"),
    {F2,[]} = string:to_float(Fs),
    F1+F2.
    0.9
    string:to_float("3/2=1.5").
    {error,no_float}
    string:to_float("-1.5eX").
    {-1.5,"eX"}
    (18)
    98> float_to_list(2.3333, [{decimals, 2}]).
    "2.33"
    float_to_list(2.333).
    "2.33300000000000018474e+00"
    list_to_float("2.3333").
    2.3333



    参考:
    http://www.erlang.org/doc/man/string.html
    http://abin888.blog.sohu.com/236274578.html
    http://blog.csdn.net/zgl_dm/article/details/5967156

  • 相关阅读:
    2014第8周二杂记
    2014第8周一JS正则小问题
    2014第7周日最强大脑
    2014第7周六杂记
    2014第7周五杂记
    2014第7周四excel多列文本复制技巧
    2014第7周三初识CouchBase
    2014第7周二需求
    2014第7周1Web安全概念学习
    shell程序之逐行读取一文件里的參数且使用此參数每次运行5分钟
  • 原文地址:https://www.cnblogs.com/ShankYan/p/4364817.html
Copyright © 2020-2023  润新知