LUA string库详解.docVIP

  • 3
  • 0
  • 约3.19千字
  • 约 4页
  • 2018-04-17 发布于河南
  • 举报
/bidepan2023/blog/item/5f49bda4468e91f09052eedc.html LUA string库详解 2009-01-09 18:33string.byte (s [, i]) Returns the internal numerical code of the i-th character of s, or nil if the index is out of range. If i is absent, then it is assumed to be?1. i may be negative. Note that numerical codes are not necessarily portable across platforms. string.char (i1, i2, ...) Receives 0 or more integers. Returns a string with length equal to the number of arguments, in which each character has the internal numerical code equal to its correspondent argument. Note that numerical codes are not necessarily portable across platforms. 1. string库中所有的字符索引从前往后是1,2,...;从后往前是-1,-2,... 2. string库中所有的function都不会直接操作字符串,而是返回一个结果 s = [abc] string.len(s)??????? ==返回5 string.rep(abc, 2) ==返回abcabc string.lower(ABC) ==返回abc string.upper(abc) ==返回ABC string.sub(s, 2)???? ==返回abc] string.sub(s, -2)??? ==返回c] string.sub(s, 2, -2) ==返回abc string.format(fmt, ...)返回一个类似printf的格式化字符串 string.find(s, pattern, pos) 第1个参数:源字符串 第2个参数:待搜索之模式串 第3个参数:A hint, 从pos位置开始搜索 找到匹配返回:匹配串开始和结束的位置,否则返回nil 简单的模式串 s = hello world i, j = string.find(s, hello) print(i, j) -- 1 5 print(string.sub(s, i, j)) -- hello print(string.find(s, world)) -- 7 11 i, j = string.find(s, l) print(i, j) -- 3 3 print(string.find(s, lll)) -- nil 格式化的模式串 s = Deadline is 30/05/1999, firm date = %d%d/%d%d/%d%d%d%d print(string.sub(s, string.find(s, date))) -- 30/05/1999 下面的表列出了Lua支持的所有字符类: . 任意字符 %s 空白符 %p 标点字符 %c 控制字符 %d 数字 %x 十六进制数字 %z 代表0的字符 %a 字母 %l 小写字母 %u 大写字母 %w 字母和数字 上面字符类的大写形式表示小写所代表的集合的补集。例如,%A非字母的字符: 模式串中的特殊字符 ( ) . % + - * ? [ ^ $ % 用作特殊字符的转义字符 %. 匹配点; %% 匹配字符 %。 转义字符 %不仅可以用来转义特殊字符,还可以用于所有的非字母的字符。当对一个字符有疑问的时候,为安全起见请使用转义字符转义他。 用[]创建字符集 [%w_] 匹配字母数字和下划线 [01] 匹配二进制数字 [%[%]]匹配一对方括号 在[]中使用连字符- %d??? 表示 [0-9]; %x??? 表示 [0-9a-fA-F] [0-7] 表示 在[]开始处使用 ^ 表示其补集: [^0-7] 匹配任何不是八进制数字的字符; [^\n] 匹配任何非换行符户的字符。 [^%s] == %S 模式修饰符 + 匹配前一字符1次或多次 * 匹配前一字符0次或多次;最长匹配 - 匹配前一字符0次或多次;最短匹配 ? 匹配前一字符0次或1次 ^ 匹配字符串开头 $ 匹配字符串结尾 捕获:用

文档评论(0)

1亿VIP精品文档

相关文档