lua 字符串处理

匹配模式(pattern)

  • . 任何单个字符
  • %a 任何字母
  • %c 任何控制字符
  • %d 任何数字
  • %g 任何除空白符外的可打印字符
  • %l 所有小写字母
  • %p 所有标点符号
  • %s 所有空白字符
  • %u 所有大写字母
  • %w 所有字母及数字
  • %x 所有 16 进制数字符号
  • %x (这里 x 是任何非字母/数字的字符) 表示字符 x。如 %% 表示百分号%,%. 表示点号.,%/ 表示斜杠/。
  • [set] 表示 set 中所有字符的联合,找到其中任一字符即表示匹配。可以用 - 连接,如[0-9] 表示 0 到 9 这十个数字。
  • [^set] 表示 set 的补集。

字符类与特殊意义符号:

  • 单个字符类跟一个 '*',将匹配零个或多个该类字符,匹配尽可能的串。
  • 单个字符类跟一个 '-',将匹配零个或多个该类字符,匹配尽可能的串。
  • 单个字符类跟一个 '+',将匹配一个或多个该类字符,匹配尽可能的串。
  • 单个字符类跟一个 '?',将匹配零个或一个该类字符。
  • %n,这里 n 可以从 1 到 9,匹配捕获的第 n 个子串。
  • %bxy,这里的 x 和 y 是两个明确的字符,匹配以 x 开始 y 结束。如 %b() 匹配包括小括号在内括起来的字符串。
  • %f[set],边境模式。匹配位于 set 内某个字符之前的一个空串,且这个位置的前一个字符不属于 set。

string.byte(s[, i[, j]])

  • 返回字符 s[i], s[i+1], ..., s[j] 的数字编码
  • i 默认为 1,j 默认等于 i

例:

str = "abcdef"
print(str:byte()) --> 97,'a' 的编码值
print(str:byte(2)) --> 98,'b' 的编码值
print(str:byte(2, 4)) -> 98 99  100

string.char(...)

  • 接收整数作为字符的编码值为参数,返回对应的字符

例:

print(string.char(97, 98, 99) --> abc

string.dump(function [, strip])

  • 以字符串形式表示一个函数,并返回。返回的字符串可用 loadstring 加载。
  • strip 为真,表示去掉函数的调试信息(局部变量名,行号等)

例:

local function print_hello()
    print("hello world")
end

local f = string.dump(print_hello, true)
print(f)

local a = loadstring(f)
print(type(a)) --> 'function'
a() --> 'hello world'

string.find(s, pattern [, init[, plain]])

  • 查找字符串 s 中第一个匹配到 pattern,返回匹配的起始和结束位置。找不到返回 nil
  • init 指定从何处开始查找,默认为 1。负值表示从倒数第几个字符开始找,直到字符串结束。
  • plain 为真,则关闭模式匹配。

例:

str = "hello, world"
print(string.find(str, "llo")) --> 3    5
print(string.find(str, "world", -5)) --> 8  12
print(string.find(str, "world", -4)) --> nil

string.format(formating, ...)

  • 同 c 里的 sprintf,不支持 *, h, L, l, n, p
  • 增加 %q,将一个字符串格式化为两个双引号括起。

例:

str = 'a string with "quotes" and\nnew line'
print(string.format("%q", str))

string.gmatch(s, pattern)

  • 返回一个迭代器函数,该函数每次被调用时都会以 pattern 为模式对 s 作匹配,并返回捕获到的值。

例:

s = "hello world from Lua"
g = string.gmatch(s, "%a+")
print(type(g)) --> 'function', g 是一个函数
print(g()) --> 'hello',调用一次则进行一次匹配
print(g()) --> 'world',返回第二次匹配的值
s = "from=world, to=Lua"
for k, v in string.gmatch(s, "(%w+)=(%w+)") do
    print(k, v) --> 打印捕获到的值
end

string.gsub(s, pattern, repl[, n])

  • 将字符串中所有匹配的 pattern 都替换成 repl,并返回替换后的字符串。第二个返回值返回匹配次数。
  • pattern 中没有设定捕获则默认捕获整个 pattern
  • 默认全部进行匹配

例:

s = "hello world, hello world, hello world"
print(s:gsub("world", "sam")) --> hello sam, hello sam, hello sam   3
  • 如果有 n 参数,则只替换前 n 个匹配。

例:

s = "hello world, hello world, hello world"
print(s:gsub("world", "sam", 1)) --> hello sam, hello world, hello world    1
print(s:gsub("world", "sam", 2)) --> hello sam, hello sam, hello world  2
  • 若 repl 是字符串,则字符串直接替换。repl 中的 %d 表示第几个捕获到的子串。%0 表示整个匹配。%%表示单个百分号。

例:

s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", "%1 %1", 1)) --> hello hello world, hello world, hello world  1
print(s:gsub("(%w+)%s*(%w+)", "%2 %1", 1)) --> world hello, hello world, hello world    1
  • 若 repl 是表,则每次匹配时都会用第一个捕获值作为键值去查找这张表。

例:

x = {}
x.hello = "HELLO"
x.world = "WORLD"

s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", x)) --> HELLO WORLD, HELLO WORLD, HELLO WORLD 6
  • 若 repl 是函数,则每次匹配时调用该函数,捕获值作为参数传入该函数。

例:

function x(str)
    return "sam"
end

s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", x)) --> sam sam, sam sam, sam sam 6
  • 表或函数的结果如果是 false 或 nil 则不操作,如果是字符串或数字,则进行替换。

例:

x = {}
x.hello = "HELLO"

s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", x)) --> HELLO world, HELLO world, HELLO world 6
function x(str)
    return nil
end

s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", x)) --> hello world, hello world, hello world 6

string.len(s)

  • 返回字符串长度

例:

print(string.len("hello, world")) --> 12

string.lower(s)

  • s 中的大写字符转成小写

例:

print(string.lower("HEllo, woRLD")) --> hello, world

string.upper(s)

  • s 中的小写字符转成大写

例:

print(string.upper("HEllo, woRLD")) --> HELLO, WORLD

string.match(s, pattern[, init])

  • 在字符串中找到第一个匹配 pattern 部分,并返回捕获值。找不到返回 nil。
  • init 指定搜索的起始位置。默认为 1,可以为负数。

例:

s = "hello world, hello world, hello world"
print(string.match(s, "hello")) --> hello
print(string.match(s, "wor%a+")) --> world

string.rep(s, n[, sep])

  • 用 sep 连接 n 个 s,并返回
  • 默认 sep 为空,即没有??? 分割符

例:

print(string.rep("hello", 2)) --> hellohello
print(string.rep("hello", 2, ", ")) --> hello, hello

string.reverse(s)

  • 反转字符串 s

例:

print(string.reverse("hello")) --> olleh

string.sub(s, i[, j])

  • 返回 s 的子串,从 i 开始,j 结束。
  • i 和 j 可以为负数。
  • j 默认为 -1,即到字符串结束。

例:

print(string.sub("hello", 2)) --> ello
print(string.sub("hello", 2, 4)) --> ell

string.pack(fmt, v1, v2, ...)

string.packsize(fmt)

string.unpack(fmt, s[, pos])

时间: 2024-07-29 15:12:44

lua 字符串处理的相关文章

Lua 字符串

Lua 字符串 字符串或串(String)是由数字.字母.下划线组成的一串字符. Lua 语言中字符串可以使用以下三种方式来表示: 单引号间的一串字符. 双引号间的一串字符. [[和]]间的一串字符. 以上三种方式的字符串实例如下: string1 = "Lua" print("\"字符串 1 是\"",string1) string2 = 'runoob.com' print("字符串 2 是",string2) strin

Lua字符串库

Lua字符串库小集 1. 基础字符串函数:    字符串库中有一些函数非常简单,如:    1). string.len(s) 返回字符串s的长度:    2). string.rep(s,n) 返回字符串s重复n次的结果:    3). string.lower(s) 返回s的副本,其中所有的大写都被转换为了小写形式,其他字符不变:    4). string.upper(s) 和lower相反,将小写转换为大写:    5). string.sub(s,i,j) 提取字符串s的第i个到第j个

Lua学习九----------Lua字符串

? 版权声明:本文为博主原创文章,转载请注明出处 1.Lua字符串 - ''单引号间的一串字符 - ""双引号之间的一串字符 - [[]]之间的一串字符 2.Lua转义字符 3.字符串操作 - string.upper(argument):将字符串全部转换成大写 - string.lower(argument):将字符串全部转换成小写 - string.gsub(mainString, findString, replaceString, num):替换字符串 - mainString

Lua字符串库(整理)

Lua字符串库小集 1. 基础字符串函数:    字符串库中有一些函数非常简单,如:    1). string.len(s) 返回字符串s的长度:    2). string.rep(s,n) 返回字符串s重复n次的结果:    3). string.lower(s) 返回s的副本,其中所有的大写都被转换为了小写形式,其他字符不变:    4). string.upper(s) 和lower相反,将小写转换为大写:    5). string.sub(s,i,j) 提取字符串s的第i个到第j个

Lua字符串

本文内容基于版本:Lua 5.3.0 TString结构 • TString结构的声明 Lua字符串对应的C结构为TString,该类型定义在lobject.h中. // lobject.h /* ** Common Header for all collectable objects (in macro form, to be ** included in other objects) */ #define CommonHeader GCObject *next; lu_byte tt; lu

Lua 字符串库函数总结

字符串库 注:字符串在Lua中是不可变的,任何的string操作都不会去改变原有的字符串,都是返回新的字符串 一.一般函数 1. 求长度 s = "Hello LUA "; print(string.len(s)); print(#s); 2. 重复 print(string.rep(s,3));   -- s 重复了3次 3. 大小写转换 print(string.upper(s)); print(string.lower(s)); -- 大小写的一个典型用途,假如要对一个字符串进行

Step By Step(Lua字符串库) (转)

1. 基础字符串函数:    字符串库中有一些函数非常简单,如:    1). string.len(s) 返回字符串s的长度:    2). string.rep(s,n) 返回字符串s重复n次的结果:    3). string.lower(s) 返回s的副本,其中所有的大写都被转换为了小写形式,其他字符不变:    4). string.upper(s) 和lower相反,将小写转换为大写:    5). string.sub(s,i,j) 提取字符串s的第i个到第j个字符.Lua中,第一

Lua字符串及字符串匹配

--lua中字符串索引从前往后是1,2,--,从后往前是-1,-2--. --string库中所有的function都不会直接操作字符串,只返回一个结果  基本函数 函数 描述 示例 结果 len 计算字符串长度 string.len("abcd") 4 rep 返回字符串s的n个拷贝(repeat重复) string.rep("abcd",2) abcdabcd lower 返回字符串全部字母大写 string.lower("AbcD") ab

Lua 字符串查找函数 string.find(s, pattern [, init [, plain]] )【转】

函数原型 string.find(s, pattern [, init [, plain]] ) s: 源字符串 pattern: 待搜索模式串 init: 可选, 起始位置 plain: 我没用过 ① 子串匹配: print(string.find("haha", 'ah') ) ----- 输出 2 3 注意: lua 里面数组或者字符串的字符, 其下标索引是从 1 开始, 不是 0 string.find 默认情况下返回两个值, 即查找到的子串的 起止下标, 如果不存在匹配返回