Lua学习九----------Lua字符串
© 版权声明:本文为博主原创文章,转载请注明出处
1.Lua字符串
- ''单引号间的一串字符
- ""双引号之间的一串字符
- [[]]之间的一串字符
2.Lua转义字符
3.字符串操作
- string.upper(argument):将字符串全部转换成大写
- string.lower(argument):将字符串全部转换成小写
- string.gsub(mainString, findString, replaceString, num):替换字符串
- mainString:要替换的字符串
- findString:将被替换的字符串
- replaceString:要替换的字符串
- num:替换次数(可以忽略,替换全部)
- string.find(str, substr, index):在一个指定的字符串中搜索指定的内容并返回其具体位置,不存在返回nil
- str:指定的字符串
- substr:指定的内容
- index:索引(默认1)
- string.reverse(arg):将字符串反转
- string.format(...):格式化字符串
- string.char(args):将整型数字转成对应的字符并连接
- string.byte(args, index):将字符串转换成对应的ASCII码,index指定字符串,默认第一位
- string.len(args):计算字符串长度
- string.rep(args, n):拷贝字符串,n表示拷贝次数
- ..:连接两个字符串
4.string.lua
a = 'This is a string' -- ''字符串 b = "This is b string" -- ""字符串 c = [[This is c string]] -- [[]]字符串 print("a:" .. a) print("b:" .. b) print("c:" .. c) d = "Escape " e = " character" print("\n\"\\a\": " .. d .. "\a" .. e) -- \a(响铃-BEL-007) print("\"\\b\": " .. d .. "\b" .. e) -- \b(退格-BS-008) print("\"\\f\": " .. d .. "\f" .. e) -- \f(换页-FF-012) print("\"\\n\": " .. d .. "\n" .. e) -- \n(换行-LF-010) print("\"\\r\": " .. d .. "\r" .. e) -- \r(回车-CR-013) print("\"\\t\": " .. d .. "\t" .. e) -- \t(水平制表-HT-009) print("\"\\v\": " .. d .. "\v" .. e) -- \v(垂直制表-VT-011) print("\"\\\\\": " .. d .. "\\" .. e) -- \\(反斜线字符-\-092) print("\"\\'\": " .. d .. "\'" .. e) -- \'(单引号字符-'-039) print("\"\\\"\": " .. d .. "\"" .. e) -- \"(双引号字符-"-034) print("\"\\0\": " .. d .. "\0" .. e) -- \0(空字符-NULL-000) str = "This is test String" result = string.upper(str) -- upper:将字符串转换成大写 print("\result = " .. result) result = string.lower(str) -- lower:将字符串转换成小写 print("result = " .. result) result = string.gsub(str, "s", "S", 2) -- gsub:替换字符串 print("result = " .. result) result = string.find(str, "is", 1) -- find:搜索指定内容,并返回下标 print("the index of \"is\" in \"" .. str .. "\":", result) result = string.reverse(str) -- reverse:字符串反转 print("result = " .. result) result = string.format("result = %s", str) -- format:格式化字符串 print(result) result = string.char(66, 77, 88, 99) -- char:将整型数字转成字符并连接 print("string.char(66, 77, 88, 99) = " .. result) result = string.byte("qAzWsx", 3) -- byte:将指定字符转化成整数值 print("string.byte(\"qAzWsx\", 2) = " .. result) result = string.len(str) -- len:计算字符串长度 print("str的长度: " .. result) result = string.rep(str, 4) -- rep:将字符串复制n次 print("result = " .. result) result = str .. " Hello" -- ..:连接两个字符串 print("result = " .. result)
5.效果预览
© 版权声明:本文为博主原创文章,转载请注明出处