lua按照标点符号或空格分割单词

本文介绍如何在Lua中利用标点符号和空格作为分隔符,有效地拆分字符串成单词,这对于文本处理和解析至关重要。通过示例代码,展示如何实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--[[
    @desc: 创建以特殊字符隔开并修改文本颜色的富文本
    --@str:完整字符串
	--@fontSize:字体大小
	--@tbColor: {[1] = 普通字体颜色, [2] = 标记字体颜色}
    --@iMaxWidth: label最大长度
    @return: node,iHeight
]]
function Tool:CreateColorText(str, iFontSize, tbColor, iMaxWidth)
    local pNode = cc.Node:create()

    local allStr = {}
    local r = {}
    if (str == nil) then
        return nil
    end
    for word in string.gmatch(str, "%P+[%s|%p]") do
        if string.find(word, " ") then
            for sub in string.gmatch(word, "%S+%s*") do
                table.insert(allStr, sub)
            end
        else
            table.insert(allStr, word)
        end
    end

	local tbHight = {}
	string.gsub(str, "「.-」", function(w)     --匹配字符串
		table.insert(tbHight, w)
	end)
    local iLineRows = iFontSize * 1.2
    local iHeight = 0
    local start = string.gsub(str, 1, 1)
    local strCurLine = ""
    local tbShow = {}
    local iLineWidth = 0
    local strTmpLine = ""
    for i=1,#allStr do
        local showcolor = tbColor[1]
        local bLight = false
		for j=1,#tbHight do
            if string.find(allStr[i], tbHight[j]) then
                allStr[i] = string.gsub(allStr[i], "[「|」]", "")
                bLight = true
				break
            end
		end
        local pTmpLabel = cc.Label:createWithSystemFont(strTmpLine..allStr[i], SYSTEM_FONT_ARIAL, iFontSize)
        local iWillLineWidth = pTmpLabel:getContentSize().width
        if bLight or iWillLineWidth > iMaxWidth or i == #allStr then
            if i == #allStr then
                strCurLine = strCurLine..allStr[i]
            end

            local pLabShow = cc.Label:createWithSystemFont(strCurLine, SYSTEM_FONT_ARIAL, iFontSize)
            pLabShow:setColor(showcolor)
            pLabShow:setPosition(cc.p(iLineWidth, iHeight))
            pNode:addChild(pLabShow)
            pLabShow:setAnchorPoint(cc.p(0, 0.5)) 
            if iWillLineWidth > iMaxWidth then
                strTmpLine = allStr[i]
                iHeight = iHeight - iLineRows
                strCurLine = allStr[i]
                iLineWidth = 0
            else
                strTmpLine = strTmpLine..allStr[i]
                iLineWidth = iWillLineWidth
            end
        else
            strTmpLine = strTmpLine..allStr[i]  
        end
        if bLight then
            showcolor = tbColor[2]
            strCurLine = allStr[i]
            local pLabShow = cc.Label:createWithSystemFont(strCurLine, SYSTEM_FONT_ARIAL, iFontSize)
            pLabShow:setColor(showcolor)
            if iWillLineWidth > iMaxWidth then                
                pLabShow:setPosition(cc.p(iLineWidth, iHeight))
                iLineWidth = pLabShow:getContentSize().width
            else
                pLabShow:setPosition(cc.p(iLineWidth - pLabShow:getContentSize().width, iHeight))
            end
            pNode:addChild(pLabShow)
            pLabShow:setAnchorPoint(cc.p(0, 0.5))
            strCurLine = ""
        elseif iWillLineWidth <= iMaxWidth then
            strCurLine = strCurLine..allStr[i]
        end
    end

    return pNode, -iHeight
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值