zoukankan      html  css  js  c++  java
  • 在Lua中计算含中文的字符串的长度

     1 --[[
     2     @desc: 计算字符串字符个数
     3     author:{author}
     4     time:2017-12-29 16:08:11
     5     --@inputstr: 源字符串
     6     return 字符个数
     7 ]]
     8 function getStringCharCount(str)
     9     local lenInByte = #str
    10     local charCount = 0
    11     local i = 1
    12     while (i <= lenInByte) 
    13     do
    14         local curByte = string.byte(str, i)
    15         local byteCount = 1;
    16         if curByte > 0 and curByte <= 127 then
    17             byteCount = 1                                               --1字节字符
    18         elseif curByte >= 192 and curByte < 223 then
    19             byteCount = 2                                               --双字节字符
    20         elseif curByte >= 224 and curByte < 239 then
    21             byteCount = 3                                               --汉字
    22         elseif curByte >= 240 and curByte <= 247 then
    23             byteCount = 4                                               --4字节字符
    24         end
    25         
    26         local char = string.sub(str, i, i + byteCount - 1)
    27         i = i + byteCount                                               -- 重置下一字节的索引
    28         charCount = charCount + 1                                       -- 字符的个数(长度)
    29     end
    30     return charCount
    31 end
  • 相关阅读:
    http uri唯一标识
    http协议
    python模块 sys
    file 文件的操作
    库的介绍及使用
    python os模块
    python的序列化与反序列化
    python 字典的定义以及方法
    python字符串的常用方法
    在Windows下使用adb logcat grep
  • 原文地址:https://www.cnblogs.com/AaronBlogs/p/8145963.html
Copyright © 2011-2022 走看看