[NAME]
ALL.dao.type.string.method

[TITLE]
字符串方法

[DESCRIPTION]

下面是字符串类型所支持的方法: 
     
   1  string( count: int, char = 0 ) => string
   2  string( count: int )[index: int => string] => string
   3  size( invar self: string, utf8 = false ) => int
   4  insert( invar self: string, str: string, pos = 0 ) => string
   5  erase( invar self: string, pos = 0, count = -1 ) => string
   6  chop( invar self: string, utf8 = 0 ) => string
   7  trim( invar self: string, where: enum<head;tail> = $head+$tail, utf8 = 0 ) => string
   8  find( invar self: string, str: string, from = 0, reverse = 0 ) => int
   9  convert( invar self: string, to: enum<local,utf8,lower,upper> ) => string
  10  replace( invar self: string, str1: string, str2: string, index = 0 ) => string
  11  expand( invar self: string, invar subs: map<string,string>|tuple<...:string>,
  12          spec = "$", keep = 1 ) => string
  13  split( invar self: string, sep = "" ) => list<string>
  14  iterate( invar self: string, unit: enum<byte,char> = $byte )[char: int, index: int]
  15  fetch( invar self: string, pattern: string, group = 0, start = 0, end = -1 )
  16      => string
  17  match( invar self: string, pattern: string, group = 0, start = 0, end = -1 )
  18      => tuple<start:int,end:int>|none
  19  change( invar self: string, pattern: string, target: string, index = 0, 
  20      start = 0, end = -1 ) => string
  21  capture( invar self: string, pattern: string, start = 0, end = -1 ) => list<string>
  22  extract( invar self: string, pattern: string, 
  23      mtype: enum<both,matched,unmatched> = $matched ) => list<string>
  24  scan( invar self: string, pattern: string, start = 0, end = -1 )
  25      [start: int, end: int, state: enum<unmatched,matched> => none|@V]
  26      => list<@V>
  27  
  28  offset( invar self: string, charIndex: int ) => int
  29  char( invar self: string, charIndex: int ) => string
     


 0.1   初始化方法  

除了可用字符串常量生成字符串外,下面的字符串初始化方法 可用来方便灵活地产生字符串。 
     
   1  string( count: int, char = 0 ) => string
   2  string( count: int )[index: int => string] => string
     


 0.1.1   string(count:int,char=0)=>string  
     
   1  string( count: int, char = 0 ) => string
     
创建并返回一个由"count"个"char"字符组成的字符串。

例子: 
     
   1  S1 = string( 5, 'S'[0] )   # SSSSS
   2  S2 = string( 3, 0x9053 )   # 道道道
     


 0.1.2   string(count:int)[index:int=>string]=>string  
     
   1  string( count: int )[index: int => string] => string
     
创建并返回一个由代码块的"count"个结果字符串连接而成的字符串。

例子: 
     
   1  S1 = string( 5 ){ "S" }       # SSSSS
   2  S2 = string( 2 ){ "道语言" }  # 道语言道语言
   3  S3 = string( 6 ){ [index] (string) index }  # 012345
     


 0.2   普通方法  

下面是可用来方便操作字符串的方法: 
     
   1  size( invar self: string, utf8 = false ) => int
   2  insert( invar self: string, str: string, pos = 0 ) => string
   3  erase( invar self: string, pos = 0, count = -1 ) => string
   4  chop( invar self: string, utf8 = 0 ) => string
   5  trim( invar self: string, where: enum<head;tail> = $head+$tail, utf8 = 0 ) => string
   6  find( invar self: string, str: string, from = 0, reverse = 0 ) => int
   7  convert( invar self: string, to: enum<local,utf8,lower,upper> ) => string
   8  replace( invar self: string, str1: string, str2: string, index = 0 ) => string
   9  expand( invar self: string, invar subs: map<string,string>|tuple<...:string>,
  10          spec = "$", keep = 1 ) => string
  11  split( invar self: string, sep = "" ) => list<string>
  12  iterate( invar self: string, unit: enum<byte,char> = $byte )[char: int, index: int]
  13  offset( invar self: string, charIndex: int ) => int
  14  char( invar self: string, charIndex: int ) => string
     


 0.2.1   size(invar self:string,utf8=false)=>int  
     
   1  size( invar self: string, utf8 = false ) => int
     
返回字符串里的字节或字符个数。

例子: 例子: 
     
   1  S = "ABCDE"
   2  len1 = S.size()
   3  len2 = "XYZ".size()
     

为了效率起见,用户也可考虑使用大小操作符%%S将给出字符串的字符个数。

 0.2.2   insert(invar self:string,str:string,pos=0)=>string  
     
   1  insert( invar self: string, str: string, pos = 0 ) => string
     
在"pos"处插入"str";
返回新字符串。

 0.2.3   erase(invar self:string,pos=0,count=-1)=>string  
     
   1  erase( invar self: string, pos = 0, count = -1 ) => string
     
从"pos"处删除"count"字节;
返回新字符串。

 0.2.4   chop(invar self:string,utf8=0)=>string  
     
   1  chop( invar self: string, utf8 = 0 ) => string
     
从字符串的末尾切除EOF, '\n', '\r'或'\r\n'。
-- EOF 将先被检查并切除;
-- '\n' 将接下来被检查并切除;
-- '\r' 将最后被检查并切除;
如果参数"utf8"非零,字符串末尾所有不构成有效UTF-8编码的字符都将被切除。

例子: 
     
   1  S1 = "line\n"
   2  S2 = S1.chop()
   3  
   4  S3 = "道语言"[:7]
   5  S4 = S3.chop( 1 )  # 道语
     


 0.2.5   trim(invarself:string,where:enum<head;tail>=$head+$tail,utf8=0)=>string  
     
   1  trim( invar self: string, where: enum<head;tail> = $head+$tail, utf8 = 0 ) => string
     
将空白字符从字符串首尾剪除。
如果参数"utf8"非零,字符串首尾所有不构成有效UTF-8编码的字符都将被剪除。

例子: 
     
   1  S1 = "\tline\n"
   2  S2 = S1.trim()         # "line"
   3  S3 = S1.trim( $head )  # "line\n"
   4  
   5  S4 = "\t道语言"[:7]
   6  S5 = S4.trim()            # "道语??"
   7  S6 = S4.trim( $tail, 1 )  # "\t道语"
     


 0.2.6   find(invar self:string,str:string,from=0,reverse=0)=>int  
     
   1  find( invar self: string, str: string, from = 0, reverse = 0 ) => int
     
从字符串位置"from"开始查找第一个出现的"str"。
如果参数"reverse"为零,向前查找,否则向后查找;
如果"str"没被找到,返回-1;否则返回,
正向查找到的子字符串的第一个字符位置;
或反向查找到的子字符串的最后那个字符的位置。

例子: 
     
   1  S1 = "dao programming language and dao virtual machine"
   2  P1 = S1.find( "dao" )         # Find the first "dao";
   3  P2 = S2.find( "dao", -1, 1 )  # Find the last "dao";
     


 0.2.7   convert(invar self:string,to:enum<local,utf8,lower,upper>)=>string  
     
   1  convert( invar self: string, to: enum<local,utf8,lower,upper> ) => string
     
转换字符串。可转换为:
-- 系统编码字符串(从UTF-8编码);
-- UTF-8编码字符串(从系统编码);
-- 全小写字符;
-- 全大写字符;

例子: 
     
   1  S1 = "Dao Language"
   2  S2 = S1.convert( $upper )  # DAO LANGUAGE
   3  S3 = S1.convert( $lower )  # dao language
     


 0.2.8   replace(invar self:string,str1:string,str2:string,index=0)=>string  
     
   1  replace( invar self: string, str1: string, str2: string, index = 0 ) => string
     
字符串"self"里的子字符串"str1"替换为str2",并返回新字符串;
如果"index"为零,替换所有的"str1";
否则,仅替换第"index"个子字符串"str1";
如果"index"为整数,查找替换将正向进行;
如果"index"为负数,查找替换将反向进行;

例子: 
     
   1  S1 = "dao programming language and dao virtual machine"
   2  S2 = S1.replace( "dao", "fast", 1 )   # replace the first "dao" with "fast";
   3  S3 = S1.replace( "dao", "fast", -1 )  # replace the last  "dao" with "fast";
   4  S4 = S1.replace( "dao", "fast", 0 )   # replace all "dao" with "fast";
     


 0.2.9   expand(invar self:string,invar subs:map<...>|tuple<...>,spec="$",keep=1)=>...  
     
   1  expand( invar self: string, invar subs: map<string,string>|tuple<...:string>,
   2          spec = "$", keep = 1 ) => string
     
将此"self"字符串里的与"subs"里的键字符串匹配的子字符串替换为对应的值字符串, 并将结果以新的
字符串返回。
如果"spec"不为空字符串,那么"subs"里的键字符必须出现在"spec"之后的括号里 才会被替换。替换时
"spec"和括号以及键字符串将被一起替换。
如果"spec"不为空且"keep"为零,那么"subs"里没有对应键的"spec(key)"将被删掉, 否则,将被保留。

例子: 
     
   1  S1 = '<a href="@(url)">@(name)</a>'
   2  keyvalues = { "url" => "http://daovm.net", "name" => "Dao Website" }
   3  S2 = S1.expand( keyvalues, "@" )  # <a href="http://daovm.net">Dao Website</a>
     


 0.2.10   split(invar self:string,sep="")=>list<string>  
     
   1  split( invar self: string, sep = "" ) => list<string>
     
将字符串以字符串"sep"分割,并将结果以字符串列表的形式返回。
如果"sep"为空字符串,"self"字符串将按单个UTF-8字符被分割。

例子: 
     
   1  S1 = "dao::io::stdio"
   2  L1 = S1.split( "::" )  # { "dao", "io", "stdio" }
   3  
   4  S2 = "道语言"
   5  L2 = S2.split()  # { "道", "语", "言" }
     


 0.2.11   iterate(invar self:string,unit:enum<byte,char>=$byte)[char:int,index:int]  
     
   1  iterate( invar self: string, unit: enum<byte,char> = $byte )[char: int, index: int]
     
历遍字符串里的每个字节或字符单元,并执行附带的代码块。
如果"unit"为"$byte",按字节历遍;
如果"unit"为"$char",按UTF-8字符历遍;
每个不构成有效UTF-8编码的字符将被分别历遍。
代码块的第一个参数可以保存字节或字符的整数编码。而第二个参数则表示 它在字符串里的位置。

例子: 
     

     


 0.2.12   offset(invar self:string,charIndex:int)=>int  
     
   1  offset( invar self: string, charIndex: int ) => int
     
获取第"charIndex"个字符的字节位置。

 0.2.13   char(invar self:string,charIndex:int)=>string  
     
   1  char( invar self: string, charIndex: int ) => string
     
获取第"charIndex"个字符。

 0.3   Pattern Matching Methods  

Please see dao.type.string.pattern for more information.

     
   1  fetch( invar self: string, pattern: string, group = 0, start = 0, end = -1 )
   2      => string
   3  match( invar self: string, pattern: string, group = 0, start = 0, end = -1 )
   4      => tuple<start:int,end:int>|none
   5  change( invar self: string, pattern: string, target: string, index = 0, 
   6      start = 0, end = -1 ) => string
   7  capture( invar self: string, pattern: string, start = 0, end = -1 ) => list<string>
   8  extract( invar self: string, pattern: string, 
   9      mtype: enum<both,matched,unmatched> = $matched ) => list<string>
  10  scan( invar self: string, pattern: string, start = 0, end = -1 )
  11      [start: int, end: int, state: enum<unmatched,matched> => none|@V]
  12      => list<@V>