lua中使用Unicode來過濾輸入字符(實例:判斷輸入字符串是否由漢字,字母,數字組成)

--(c>=48 and c<=57)數字

--(c>= 65 and c<=90)大寫字母

--(c>=97 and c<=122)小寫字母

--判斷是否由漢字,大小寫字母組成

function Balance:GetStringA_Z_Chinese(str)

    local ss = {}

    local k = 1

    while true do

        if k > #str then

            return true

        end

        local c = string.byte(str, k)

        if not c then

            return false

        end

        if c < 192 then

            if (c >= 65 and c <= 90) or (c >= 97 and c <= 122) then

                table.insert(ss, string.char(c))

            else

                return false

            end

            k = k + 1

        elseif c < 224 then

            k = k + 2

            return false

        elseif c < 240 then

            if c >= 228 and c <= 233 then

                local c1 = string.byte(str, k + 1)

                local c2 = string.byte(str, k + 2)

                if c1 and c2 then

                    local a1, a2, a3, a4 = 128, 191, 128, 191

                    if c == 228 then

                        a1 = 184

                    elseif c == 233 then

                        a2, a4 = 190, c1 ~= 190 and 191 or 165

                    end

                    if c1 >= a1 and c1 <= a2 and c2 >= a3 and c2 <= a4 then

                        table.insert(ss, string.char(c, c1, c2))

                    else

                        return false

                    end

                else

                    return false

                end

            else

                return false

            end

            k = k + 3

        elseif c < 248 then

            k = k + 4

            return false

        elseif c < 252 then

            k = k + 5

            return false

        elseif c < 254 then

            k = k + 6

            return false

        end

    end

end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章