lua字符串


local s = "hello abc"

print(string.sub(s,1,3))

print(string.find(s,"o"))--start and end index(from 1)

local gs = string.gsub("abc","a","b")--replace a to b
print(gs)

hel
5 5
bbc



local h = "hello "
local w = "world "
print(h..w)
local str = string.format("%s %s,%d,%.2f","hello","world",123,123)
print(str)

hello world
hello world,123,123.00



print(10+1+"1")
print(tostring(1))

print("input a num:")
line = io.read()
num = tonumber(line)
if num==nil then
print("input error")
else
print(num)
end

12
1
input a num:
2
2


字符操作

print(string.byte('abc',1,2))
print(string.char(97,98))

97 98
ab


gmatch:返回一个迭代器函数,通过这个迭代器函数可以遍历到在字符串s中出现模式串p的所有地方。

s = "hello world "
for w in string.gmatch(s, "%a+") do --匹配最长连续且只含字母的字符串
print(w)
end

hello
world


gsub(s, p, r [, n]):将目标字符串s中所有的子串p替换成字符串r。可选参数n,表示限制替换次数

print(string.gsub("hello world,world","world","china"))

hello china,china 2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值