原文地址http://www.freecls.com/a/2712/f

lua的string库是用来处理字符串的,基础函数如下

string.byte(s [, i [, j]])

string.byte是用来把字符转换成ascii数字,s为目标字符串,i为索引开始位置(从1开始),j为索引结束位置

string.char(...)

string.char是把ascii数值转换成字符

例子

--默认为第1个返回a的ascii值
    local r = string.byte('abcdefg')    --97
     
    --从索引2(b)到索引4(d)也就是分别返回bcd的ascii值
    local r1,r2,r3 = string.byte('abcdefg',2,4)    --98,99,100
     
    --返回98所对应的字符
    local r = string.char(98)    --a
     
    --返回98,,99,100对应的字符并连在一起返回
    local r = string.char(98,99,100)    --abc

string.sub (s, i [, j])

截取字符串(字符串分割,字符串截取),i为起始索引,可选参数j为结束索引(包含),都可以为负数,第一个字符索引为1,最后一个字符为-1

例子

local res,s
    s = 'www.freecls.com'
    res = string.sub(s,5)     --freecls.com
    res = string.sub(s,5,-1)  --freecls.com
     
    --截取后3位
    res = string.sub(s,-3)    --com
     
    --截取前3位
    res = string.sub(s,1,3)   --www

string.dump(function)

把函数序列化成字符串来保存那么下次要使用的时候直接用loadstring或loadfile就可以还原函数

例子

function say()
        print('hello')
    end
     
    local f_str = string.dump(say)
    print(f_str)    --uaQ
     
    --复原函数
    local func = loadstring(f_str)
    func()
     
    --如果我们把f_str保存到了文件tmp.txt则可以用loadfile('tmp.txt')来还原函数

string.find (s, pattern [, init [, plain]])

字符串查找函数找不到返回nil,找到了返回开始位置和结束位置,init为从哪里开始默认为1,plain默认为false表示利用模式匹配,如果设为true则表示纯文本匹配(也就是关闭正则匹配)

例子

local str = 'i love programming,11,22,%d+aa'
    local s = string.find(str,'222')    --nil
    s = string.find(str,'pro')  --8
    s = string.find(str,",%d+")    --19(匹配到了,11)
    s = string.find(str,",%d+",1,true)    --25(由于关闭了模式匹配,所以匹配到了,%d+)

string.match (s, pattern [, init])

它跟string.find差不多,只不过能把捕获匹配到的结果并返回

例子

local s,res,res1,res2
    s = 'http://www.freecls.com'
     
    --由于没有捕获,返回全部匹配
    --结果:http://www.freecls.com
    res = string.match(s,'http://%a+\.%a+\.com')
     
    --如果有捕获,则分别返回捕获结果
    --结果:www    freecls
    res1,res2 = string.match(s,'http://(%a+)\.(%a+)\.com')

string.gsub (s, pattern, repl [, n])

用来做字符串替换,可选参数n代表替换多少次默认全部替换,返回替换后的字符串

例子

local s,res,res1,res2
    s = 'http://www.freecls.com'
     
    --结果:http://test.freecls.com
    res = string.gsub(s,'www','test')
     
    --捕获替换
    --结果:test.freecls.abc
    res = string.gsub(s,'^http://%w+\.(%w+)\.com$','test.%1.abc')
     
    --w替换成t,但是只替换2次
    --结果:http://ttw.freecls.com
    res = string.gsub(s,'w','t',2)

string.gmatch (s, pattern)

迭代匹配

例子

local s = 'www.freecls.com'
    words = {}
    for w in string.gmatch(s, "%a+") do
        words[#words + 1] = w
    end
    --words最终结果为
    --{'www','freecls','com'}

string.format (formatstring, ···)
字符串格式化类型c语言的sprintf不说废话以例子来讲解

local s = string.format('%d%s',123,'freecls')   --123freecls
     
    s = string.format('%0.2f',1.234343)     --1.23(保留2位)
     
    --转成16进制,%X为大写的16进制
    local s = string.format('%X',140)       --8C
    local s = string.format('%x',140)       --8c
    local s = string.format('%04x',140)     --008c

string.len(s)

返回字符串长度=#s

string.rep(s,n)

字符串重复n次并拼接返回

string.lower(s)

转小写

string.upper(s)

转大写

string.reverse(s)

反转字符串

总结

1.本文还有很多涉及正则表达式知识,在这里不做介绍,将会额外讲解  
2.本文只是对string库做简单的介绍,如果有疑问可以给我留言
3.lua的版本为5.1,运行环境centos7 64位
4.原文地址http://www.freecls.com/a/2712/f
---------------------
作者:戴磊freecls
来源:CSDN
原文:https://blog.csdn.net/freecls/article/details/80264398
版权声明:本文为博主原创文章,转载请附上博文链接!

lua字符串处理(string库用法)的更多相关文章

  1. Lua 中的string库(字符串函数库)总结

    (字符串函数库)总结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-11-20我要评论 这篇文章主要介绍了Lua中的string库(字符串函数库)总结,本文讲解了string库 ...

  2. go中字符串类型string的用法

    示例 // 字符串类型string的用法 package main import ( "fmt" "unsafe" ) func main() { // 字符串 ...

  3. Swift - 字符串(String)用法详解

    下面对String常用的属性和方法做个总结 1,判断是否为空:isEmpty 1 2 3 var str:String if str.isEmpty{ } 2,获取字符数量:countElements ...

  4. Lua的string和string库总结

    Lua有7种数据类型,分别是nil.boolean.number.string.table.function.userdata.这里我总结一下Lua的string类型和string库,复习一下,以便加 ...

  5. Lua字符串库(整理)

    Lua字符串库小集 1. 基础字符串函数:    字符串库中有一些函数非常简单,如:    1). string.len(s) 返回字符串s的长度:    2). string.rep(s,n) 返回 ...

  6. Step By Step(Lua字符串库)

    Step By Step(Lua字符串库) 1. 基础字符串函数:    字符串库中有一些函数非常简单,如:    1). string.len(s) 返回字符串s的长度:    2). string ...

  7. Lua string库整理

    string库提供了字符串处理的通用函数. 例如字符串查找.子串.模式匹配等. 当在 Lua 中对字符串做索引时,第一个字符从 1 开始计算(而不是 C 里的 0 ). 索引可以是负数,它指从字符串末 ...

  8. Lua 之string库

    标准string库 基础字符串函数 string.len(s) 返回一个字符串的长度,例如 string.rep(s, n) 返回一个新的字符串,该字符串是参数s重复n次得到的结果,例如 )) -- ...

  9. 在lua的string库和正则表达式

    一.前提要了解一下lua 的string几个方法 1. string库中所有的字符索引从前往后是1,2,...;从后往前是-1,-2,... 2. string库中所有的function都不会直接操作 ...

随机推荐

  1. 推荐系统(5)---大量项目topk近邻相似度

    Kd树+BBF(最邻近.次邻近查询)Python实现 kd树和BBF算法 精确Top-K检索及其加速方法探讨

  2. Codeforces Round #609 (Div. 2) C. Long Beautiful Integer

    链接: https://codeforces.com/contest/1269/problem/C 题意: You are given an integer x of n digits a1,a2,- ...

  3. vulkan load store and memoryless

    https://www.jendrikillner.com/article_database/ https://community.arm.com/developer/tools-software/g ...

  4. win10 水晶报表安装包

    windows 10 64 VS2013安装 CR For VS 13_0_18 安装过程没有报错 安装成功http://downloads.businessobjects.com/akdlm/cr4 ...

  5. BZOJ 3698: XWW的难题(有源汇上下界最大流)

    题面 XWW是个影响力很大的人,他有很多的追随者.这些追随者都想要加入XWW教成为XWW的教徒.但是这并不容易,需要通过XWW的考核. XWW给你出了这么一个难题:XWW给你一个N*N的正实数矩阵A, ...

  6. nginx location中root指令和alias指令的区别

    nginx location中root指令和alias指令 功能:将url映射为文件路径,以返回静态文件内容 差别:root会将完整的url映射进文件路径中 alias只会将location后的url ...

  7. IDEA八条配置修改

    IDEA版本:IntelliJ IDEA 2019.2.1 x64 八条配置修改: 自动编译开关 忽略大小写开关 智能导包开关 悬浮提示开关 取消单行显示tabs的操作 项目文件编码 滚轴修改字体大小 ...

  8. 学到了林海峰,武沛齐讲的Day19 迭代细讲

    在家加1个月学了8day的课  出差6天看了8day的课..说明再忙也是可以挤挤多学习的. 广州出差最后两天没学习.一天做车,一天做公司的事...4天就过去了. 老师讲的包子和鸡蛋需求不好...讲的有 ...

  9. msf爆破

    SSH服务口令猜解: msf > use auxiliary/scanner/ssh/ssh_loginmsf auxiliary(ssh_login) > show optionsmsf ...

  10. 四十.创建Redis集群 管理集群

    环境准备 准备 6台(51-56) redis服务器  以默认配置运行redis服务即可  一.创建Redis集群 1.启用集群功能( 51-56 都要配置) ]#  netstat -antupl ...