Lua标准库中的字符串库提供了一些常用的字符串处理函数,包括字符串的查找、替换、格式化等功能。下面是一些常用的字符串库函数及其用法:

  1. string.len(s) - 返回字符串 s 的长度
local s = "hello world"
print(string.len(s))  -- 输出 11
  1. string.upper(s) - 将字符串 s 中的所有字母转换为大写
local s = "hello world"
print(string.upper(s))  -- 输出 HELLO WORLD
  1. string.lower(s) - 将字符串 s 中的所有字母转换为小写
local s = "HELLO WORLD"
print(string.lower(s))  -- 输出 hello world
  1. string.sub(s, start, end) - 返回字符串 s 中从索引 start 到 end 的子串
local s = "hello world"
print(string.sub(s, 1, 5))  -- 输出 hello
  1. string.find(s, pattern) - 在字符串 s 中查找 pattern,并返回其起始位置和结束位置
local s = "hello world"
local start, stop = string.find(s, "world")
print(start, stop)  -- 输出 7 11
  1. string.gsub(s, pattern, replace) - 在字符串 s 中查找 pattern,并用 replace 替换
local s = "hello world"
local new_s = string.gsub(s, "world", "Lua")
print(new_s)  -- 输出 hello Lua
  1. string.format(format, …) - 格式化字符串
local name = "Alice"
local age = 20
local str = string.format("My name is %s and I am %d years old", name, age)
print(str)  -- 输出 My name is Alice and I am 20 years old

以上是一些常用的字符串库函数及其用法,你可以根据实际需求选择合适的函数来处理字符串。Lua的字符串库还提供了更多的函数,你可以查看官方文档了解更多细节。