字符串处理
1.把字符串转换成数字
mystring = "1234"
myNumber = tonumber(mystring)
print(mystring)
print(type(myNumber))
2.把数字转换成字符串
myNumber = 123
mystring = tostring(myNumber)
print(type(mystring))
print(mystring)
3.字符串的长度
mystring = "hello"
print("字符串的长度",string.len(mystring))
print("字符串的长度",#mystring)
4.返回指定字符串
mystring = "hello world"
newstring = string.sub(mystring,1,7)
print(newstring)
4.1 从字符串的尾部开始截取
mystring = "hello world"
newstring = string.sub(mystring,-5)
print(newstring)
5.格式化字符串
str1 = "hello"
str2 = " world"
str3 = string.format("%s%s",str1,str2)
print(str3)
5.1
pai = 3.1415926
print(string.format("%.2f",pai))
5.2
str = "你好吗" --一个中文占三个字节
print(#str)
6.string.find 查找字符串,返回位置
TVB = {}
function TVB.FindStirng(originString,pattern)
return string.find(originString,pattern)
end
mystring = "hello world"
print(TVB.FindStirng(mystring,"hello"))
6.1 查找字符串其他的用法
mystring = "共消费RMB1980.656,折后价格为1980"
pattern = "%a%a%a%d%d%d%d.%d%d%d"
print(string.sub(mystring,string.find(mystring,pattern)))
6.2 查找话题 #话题#
mystring = "#话题#一个话题@啦啦啦"
pattern = "#[^%s+]+#"
for str in string.gfind(mystring,pattern) do
print(str)
end