lua字符串处理(string库用法)
原文地址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库用法)的更多相关文章
- Lua 中的string库(字符串函数库)总结
(字符串函数库)总结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-11-20我要评论 这篇文章主要介绍了Lua中的string库(字符串函数库)总结,本文讲解了string库 ...
- go中字符串类型string的用法
示例 // 字符串类型string的用法 package main import ( "fmt" "unsafe" ) func main() { // 字符串 ...
- Swift - 字符串(String)用法详解
下面对String常用的属性和方法做个总结 1,判断是否为空:isEmpty 1 2 3 var str:String if str.isEmpty{ } 2,获取字符数量:countElements ...
- Lua的string和string库总结
Lua有7种数据类型,分别是nil.boolean.number.string.table.function.userdata.这里我总结一下Lua的string类型和string库,复习一下,以便加 ...
- Lua字符串库(整理)
Lua字符串库小集 1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string.rep(s,n) 返回 ...
- Step By Step(Lua字符串库)
Step By Step(Lua字符串库) 1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string ...
- Lua string库整理
string库提供了字符串处理的通用函数. 例如字符串查找.子串.模式匹配等. 当在 Lua 中对字符串做索引时,第一个字符从 1 开始计算(而不是 C 里的 0 ). 索引可以是负数,它指从字符串末 ...
- Lua 之string库
标准string库 基础字符串函数 string.len(s) 返回一个字符串的长度,例如 string.rep(s, n) 返回一个新的字符串,该字符串是参数s重复n次得到的结果,例如 )) -- ...
- 在lua的string库和正则表达式
一.前提要了解一下lua 的string几个方法 1. string库中所有的字符索引从前往后是1,2,...;从后往前是-1,-2,... 2. string库中所有的function都不会直接操作 ...
随机推荐
- C++——虚函数表解析
转自:https://blog.csdn.net/haoel/article/details/1948051 前言 C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用父类型指针指 ...
- 剖析可执行文件ELF组成
对比参考:剖析.o文件ELF组成 相比.o的ELF格式,有哪些变化? .rel.text和.rel.data消失了 为什么这两个节会消失? 链接器将各.o中同名的.text和.data节整合到一起时, ...
- LRU(最近最少使用)(python实现)
""" python3 only LRU cache """ from collections import OrderedDict fro ...
- 遍历SQL SERVER中所有存储过程和触发器
如果需要查找某个存储过程或触发器中是否含有某段文本(比如:你想知道有哪些存储过程操作了某个表) 可以这么写 select name from sysobjects o, syscomments s w ...
- js 异步执行顺序
参考文章: js 异步执行顺序 1.js的执行顺序,先同步后异步 2.异步中任务队列的执行顺序: 先微任务microtask队列,再宏任务macrotask队列 3.调用Promise 中的res ...
- Jquery Ajax跨域访问
一.同源策略 二.跨域的集中方法: 1.服务器端发送请求,服务器作为中继代理(此方法不理解) 2.iframe 3.script标签 通过动过动态生成script标签,并将src指向目标源的方式(im ...
- flask 框架 转载:https://cloud.tencent.com/developer/article/1465968
特点总结: 类名称---->数据库表名 类属性---->数据库字段 类的对象----->数据库表中的一行一行数据 3.ORM操作注意(理解) 1/因为SQLALChemy去app身上 ...
- TCP单线程实现并发
服务端 from gevent import monkey;monkey.patch_all() import socket from gevent import spawn server = soc ...
- jquery显示隐藏密码跟显示密码
今天讲述的是html5中input的password密码的加密与显示 都知道input标签加上password输入密码显示的都是原点.......怎么点一个按钮让他显示回来明文数字1234567 上代 ...
- python 高阶函数之filter
前文说到python高阶函数之map,相信大家对python中的高阶函数有所了解,此次继续分享python中的另一个高阶函数filter. 先看一下filter() 函数签名 >>> ...