Lua字符相关基本函数
a = "hello world"
print("a is :",a)
print("转换为大写:",string.upper(a))
print("转换为小写:",string.lower(a))
print(string.gsub(a,"world","time",1)) --将world替换为time
print("寻找a中的world的位置:",string.find(a,"world",1))
print("反转",string.reverse(a))
print(string.format("hello %s","world"))
print("char型转换为字符型",string.char(97,98,99,100))
print("第六个字母的ASCII值",string.byte(a,6))
print("h的ascii值==========="..string.byte(a))
print("字符串长度-----------"..string.len(a)) --字符串长度
print("返回字符串2次========"..string.rep(a,2)) --返回字符串a的2个拷贝
print("拼接-----------------"..a .. "!")
print("=========================================================")
for str in string.gmatch(a,"%a") --返回符合第二个参数的字符,无符合条件返回nil
do
print(str)
end
print("----------------------------------------------------------")
print(string.match("I have 2 questions for you.","%d+ %a+"))
print("--------------截取-----------------------------------------")
print(string.sub(a,1,7))
运行结果: