来源http://codepad.org/BRHIS6WA

 --根据首字节获取UTF8需要的字节数
 local function GetUTF8CharLength(ch)
     local utf8_look_for_table = {
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
         , , , , , , , , , , , , , , , ,
     }
     return utf8_look_for_table[ch]
 end

 --根据UTF8流获取字符串长度
 --GetUTF8Length("一二三四五六七") 返回7
 local function GetUTF8Length(str)

     repeat
         local char = string.byte(str, ptr)
         local char_len = GetUTF8CharLength(char)
         len = len +
         ptr = ptr + char_len
     until(ptr>#str)
     return len
 end

 --截取UTF8字符串
 --SubUTF8String("一二三四五六七",1,3) 返回一二三
 local function SubUTF8String(str, begin, length)
     begin = begin
     length = length  --length为-1时代表不限制长度
     local ret = ""

     repeat
         local char = string.byte(str, ptr)
         local char_len = GetUTF8CharLength(char)
         len = len + 

          or len<begin+length) then
             ,char_len- do
                 ret = ret .. string.char( string.byte(str, ptr + i) )
             end
         end

         ptr = ptr + char_len
     until(ptr>#str)
     return ret
 end

 local function test(str, len)
     local ret = {}
     ,math.ceil(GetUTF8Length(str)/len) do
         ret[i] = SubUTF8String(str, (i-)*len + , len)
     end
     return ret
 end

 local str = "游戏程序员们是一群对技术最精益求精、心理素质最好、工作最勤奋的人!"
 ))do
 print(k,v)
 end
Output:

    游
    戏
    程
    序
    员
    们
    是
    一
    群
    对
    技
    术
    最
    精
    益
    求
    精
    、
    心
    理
    素
    质
    最
    好
    、
    工
    作
    最
    勤
    奋
    的
    人
    !

lua分割字符串终究版(转载,有改动)的更多相关文章

  1. lua分割字符串

    str = "abc;123;345" local tab = string.split(str, ";") 然后list里面就是 abc123345 了.第二 ...

  2. lua 分割字符串

    -- 参数:待分割的字符串,分割字符 -- 返回:子串表.(含有空串) function split(str, split_char) local sub_str_tab = {} while tru ...

  3. 【转载】C#使用Split函数根据特定分隔符分割字符串

    在C#程序开发过程中,很多时候可能需要将字符串根据特定的分割字符分割成字符或者List集合,例如根据逗号将字符串分割为数组,或者根据竖线将字符串分割成数组,C#中提供了Split()函数来快速将字符串 ...

  4. lua string的自定义分割字符串接口

    -------------------------------------------------------------------- --  Create By  SunC 2014/7/1 -- ...

  5. SQLServer实现split分割字符串到列

    网上已有人实现sqlserver的split函数可将字符串分割成行,但是我们习惯了split返回数组或者列表,因此这里对其做一些改动,最终实现也许不尽如意,但是也能解决一些问题. 先贴上某大牛写的sp ...

  6. Python:笔记1_字符串处理【转载】

    [转载自:https://www.cnblogs.com/houht/p/3308634.html] 1. 判断字符串str是否为空Approach 1:如果字符串长度为0,说明字符串为空,code如 ...

  7. Chromium base库分割字符串SplitString

    前一段时间在工作过程中遇到一个场景需要将http response中的request header中的cookie字段取出并进行解析,但是手头没有解析cookie的工具类,同时cookie的表现就是个 ...

  8. Delphi中stringlist分割字符串的用法

    Delphi中stringlist分割字符串的用法 TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 1.CommaT ...

  9. C语言分割字符串

    最近在做一道C语言题目的时候需要用到分割字符串,本来想自己手写的,也不会很麻烦,但想到其他语言都有分割字符串的库函数,C语言怎么会没有呢?所以,在网上搜了一搜,果然有这样的函数,还是很好用的,在此总结 ...

随机推荐

  1. Windows Azure Platform 系列文章目录

    Windows Azure Platform (一) 云计算的出现 Windows Azure Platform (二) 云计算的分类和服务层次 Windows Azure Platform (三) ...

  2. Systemd Unit文件中PrivateTmp字段详解-Jason.Zhi

    如下图,在开发调试的时候会遇到这么一个问题. file_put_contents时,$tmp_file显示的目标文件是/tmp/xxx.而这个文件实际放在linux的目录却是/tmp/systemd- ...

  3. Bubble Sort

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  4. nova 配置文件

    控制节点 /etc/nova/nova.conf [DEFAULT]cpu_allocation_ratio=32.0service_down_time = 7200#use_local = True ...

  5. C++11之使用或禁用对象的默认函数

    [C++11之使用或禁用对象的默认函数] C++11 允许显式地表明采用或拒用编译器提供的内置函数.例如要求类型带有默认构造函数,可以用以下的语法: 另一方面,也可以禁止编译器自动产生某些函数.如下面 ...

  6. Options for Debugging Your Program or GCC

    [Options for Debugging Your Program or GCC] -g Produce debugging information in the operating system ...

  7. spring mvc中的valid

    当你希望在spring mvc中直接校验表单参数时,你可以采用如下操作: 声明Validator的方式: 1.为每一个Controller声明一个Validator @Controller publi ...

  8. USB开发库文件分析

    stm32f10x_it.c: 该文件中包含 USB 中断服务程序,由于 USB 中断有很多情况,这里的中断服务程序只是调用 usb_Istr.c 文件中的 USB_Istr 函数,由 USB_Ist ...

  9. hdoj 5371 Hotaru's problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 这道题用到了Manacher算法,首先简单介绍一下Manacher算法: ----------- ...

  10. KMP算法初探

    [edit by xingoo] kmp算法其实就是一种改进的字符串匹配算法.复杂度可以达到O(n+m),n是参考字符串长度,m是匹配字符串长度. 传统的算法,就是匹配字符串与参考字符串挨个比较,如果 ...