第四章:Perl6 的字符串
第四章:Perl6 的字符串 每个表达式都有一个隐含的 perl 方法,来打印出 perl6 语言相应的表达能力, 当然也可以自己设计其余的表达能力。例如 .spp .javascript 等。 Perl6 是处理语言的语言,所以其最强大的功能都体现在处理字符串的能力上,这继承了 Perl5 的财富。 字符串有几种表示形式,单引号中除了反斜杠的需要转义外,其他都是本来的含义, 双引号中不但可以内插变量,还可以有许多特殊符号的表示,还有许多不经常用到的字符串形式: 'string' "send me a $message" 字符串是不能修改的,除非用新的值替换它: > my $x = 'hello' hello > chop $x hell > say $x hello > $x = chop $x hell > say $x hell 所有的关于字符串的函数都有两(三)种表示方法: 'string'.chop; chop 'string'; chop('string'); 函数 chomp剔除字符串结尾的回车符,在 Unix 和 Windows 系统中的回车符都可以一次性的剔除: > "strn".chomp.perl "str" > "strr".chomp.perl "str" > "strrn".chomp.perl "str" > "strnr".chomp.perl "strn" 函数 chop剔除结尾指定长度的字符,默认为 1 个,小于 1 或大于字符串长度的截取都是无效的: > "string".chop strin > "string".chop(2) stri > "string".chop(0) string > "string".chop(6) > "string".chop(7) string > "string".chop(-1) string 函数 lc将字符串中大写的部分转换成小写,Perl6 是一门大小写敏感的语言: > 'HELLO'.lc hello > 'hellO'.LC Method 'LC' not found for invocant of class 'Str' > 'hellO'.lc hello > '123'.lc 123 函数 uc将字符串中小写的部分转换成大写: > 'hello'.uc HELLO > 'HEllo'.uc HELLO 函数 tc将首字符大写: > 'hello'.tc Hello > 'HELLO'.tc HELLO 函数 tclc将首字母大写,其余的小写: > 'hello'.tclc Hello > 'HELLO'.tclc Hello 函数 wordcase将每个单词都进行 tclc 的操作: > 'hello world'.wordcase Hello World 函数 chars获取字符串的长度: > 'hello'.chars 5 函数 index获取字符串指定位置和长度的子字符串: say index "Camelia is a butterfly","a"; # 1 say index "Camelia is a butterfly","a",2; # 6 say index "Camelia is a butterfly","er"; # 17 say index "Camelia is a butterfly","Camel"; # 0 say index "Camelia is a butterfly","Onion"; # Int() say index("Camelia is a butterfly","Onion").defined ?? 'OK' !! 'NOT'; # NOT 函数 rindex从后往前获取指定子字符串在字符串中的位置: > 'hello'.rindex('l') 3 > 'hello'.rindex('h') 0 > 'hello'.rindex('o') 4 函数 split根据指定字符或匹配拆分字符串: > 'a;b;c'.split(';').perl ("a","b","c").list > 'a;b;c'.split(';',:all).perl (("a",";"),("b",2).perl ("a","b;c").list > 'a;b;c'.split(';',2,"b;c").list > 'a,b;c'.split(/<[;,]>/).perl ("a","c").list 函数 comb获取字符串中匹配指定模式的子字符串列表(注:列表只有一个元素时,会多一个逗号): > 'a;b;c'.comb(/w/).perl ("a","c").list > 'a;b;c'.comb(/N/).perl ("a",";","c").list > 'ad;bd;cd'.comb(/w+/).perl ("ad","bd","cd").list > 'ad;bd;cd'.comb(/w+/,2).perl ("ad","bd").list > 'ad;bd;cd'.comb(/w+;w+/).perl ("ad;bd",).list 函数 lines按照回车拆分字符串,也就是按照行来分解字符串: > "anb".lines.perl ("a","b").list > "an".lines.perl ("a",).list > "ann".lines.perl ("a","").list > "annn".lines.perl ("a","","").list 函数 words将字符串拆分成单词 > "anbn".words.perl ("a","b").list > "hello world".words.perl ("hello","world").list > "foo:bar".words.perl ("foo:bar",).list > "foo:bartbaz".words.perl ("foo:bar","baz").list 函数 flip反转字符串 > 'hello'.flip olleh > 'abba'.flip abba 函数 subst使用一个匹配或字符串替换字符串: > 'some foo'.subst(/foo/,'string') some string > 'the answer is secret'.subst(/secret/,{++$i}) the answer is 1 > "Hey foo foo foo".subst(/foo/,'bar',:g) Hey bar bar bar > "Hey foo foo foo".subst(/foo/,:x(0)) Hey foo foo foo > "Hey foo foo foo".subst(/foo/,:x(1)) Hey bar foo foo > "Hey foo foo foo".subst(/foo/,:nth(3)) Hey foo foo bar 函数 substr返回字符串指定位置指定长度的子字符串: substr("Long string",6,3); # tri substr("Long string",6); # tring substr("Long string",*-1); # trin substr("Long string",*-3,*-1); # in 函数 succ返回字符串代表的数字加1: '12.34'.succ # 13.34 'img001.png'.succ # img002.png 'aa'.succ # ab 'az'.succ # ba '109'.succ # 110 'α'.succ # β 'a9'.succ # b0 函数 pred将字符串中的数字递减(尤其用在文件名的生成上): 'b0'.pred # a9 'a0'.pred # Failure 'img002.png'.pred # img001.png 函数 ord返回字符串第一个字符的 codepoints (ASC 值): > 'str'.ord 115 > 'string'.ord 115 > 'string'.ord(2) Too many positionals passed; expected 1 argument but got 2 > 's'.ord 115 函数 ords返回字符串每个字符的 codepoints(ASC 值): > 'str'.ords 115 116 114 > 'str'.ords.perl (115,116,114).list 函数 indent将字符串每行缩进指定的字符: > "hello world".indent Too few positionals passed; expected 2 arguments but got 1 (应当设置默认的缩进为0) > "hello world".indent(2).perl " hello world" > " hello world".indent(2).perl " hello world" > " hello worldnthanks".indent(2).perl " hello worldn thanks" 函数 trim剔除字符串首尾的空格: > ' hello world '.trim.perl "hello world" > (trim ' hello world ').perl "hello world" 函数 trim-trailing剔除字符串后后面的空格: > 'tanks '.trim-trailing tanks 函数 trim-leading剔除字符串开始的空格: > ' tanks'.trim-leading tanks (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |