电晕string.find():找到“.". [英] Corona string.find() : Finding "."

查看:89
本文介绍了电晕string.find():找到“.".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串分成两部分,并用'.'字符将其分开.但是string.find()函数无法处理

I'm trying to split a string into two parts, which is divided by a '.' character. But string.find() function cant handle that

我有这种琴弦

local test = "345345.57573"

我尝试过

local start = string.find( test, "." )
local start = string.find( test, "\." )
local start = string.find( test, "(%w+).(%w+)" )

但是他们都不起作用. String.find()始终返回1,这是错误的. 可能是什么问题?

But none of them worked. String.find() always return 1 which is false. What might be the problem?

我也尝试使用gsub和change.与另一个角色,但也不起作用

I also tried to use gsub and change . with another character but it didn't work either

推荐答案

尝试以下示例

function split(pString, pPattern)

    if string.find(pString,".") then
        pString = string.gsub(pString,"%.","'.'")
    end

    if pPattern == "." then
        pPattern = "'.'"
    end

    local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
    local fpat = "(.-)" .. pPattern
    local last_end = 1
    local s, e, cap = pString:find(fpat, 1)
    while s do
        if s ~= 1 or cap ~= "" then
            table.insert(Table,cap)
        end
        last_end = e+1
        s, e, cap = pString:find(fpat, last_end)
    end
    if last_end <= #pString then
        cap = pString:sub(last_end)
        table.insert(Table, cap)
    end

    return Table
end

local myDataTable = split("345345.57573",".")

--Loop Through and print the last split data table

print(myDataTable[1]) --345345
print(myDataTable[2]) --57573

参考

这篇关于电晕string.find():找到“.".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆