lua拆分string类型数字

local function split1(str, delimiter)
    if str==nil or str=='' or delimiter==nil then
        return nil
    end
    
    local result_str = {}
    for match in (str..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result_str, match)
    end

    local result_num = {}
    for i = 1, #result_str do
        table.insert(result_num,tonumber( result_str[i] ))
    end
    return result_num
end
调用:
split1("1,2,3,4",",")

改进后,
function split( str )
    -- body
    result = {}
    for i = 1, string.len(str) do
        table.insert(result,tonumber(string.sub(str,i,i)))
    end
    return result
end
调用:
split("1234")

Lua中的字符串是不可变的,即一旦创建就不能修改。可以使用字符串库中的函数来操作字符串。以下是一些常用的Lua字符串函数: 1. string.sub(s, i, j):返回字符串s中从第i个字符到第j个字符之间的子串。 ```lua local str = "hello world" print(string.sub(str, 1, 5)) -- 输出:hello ``` 2. string.rep(s, n):返回字符串s重复n次的结果。 ```lua local str = "hello" print(string.rep(str, 3)) -- 输出:hellohellohello ``` 3. string.reverse(s):返回字符串s的反转字符串。 ```lua local str = "hello" print(string.reverse(str)) -- 输出:olleh ``` 4. string.lower(s):返回字符串s的小写字符串。 ```lua local str = "Hello World" print(string.lower(str)) -- 输出:hello world ``` 5. string.upper(s):返回字符串s的大写字符串。 ```lua local str = "Hello World" print(string.upper(str)) -- 输出:HELLO WORLD ``` 6. string.gsub(s, pattern, replace):返回字符串s中所有匹配pattern的子串被替换为replace后的结果。 ```lua local str = "hello world" print(string.gsub(str, "o", "a")) -- 输出:hella warld ``` 7. string.find(s, pattern):返回字符串s中第一个匹配pattern的子串的起始位置和结束位置。 ```lua local str = "hello world" local start, finish = string.find(str, "world") print(start, finish) -- 输出:7 11 ``` 8. string.match(s, pattern):返回字符串s中第一个匹配pattern的子串。 ```lua local str = "hello world" print(string.match(str, "world")) -- 输出:world ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值