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都不会直接操作 ...
随机推荐
- leetcode刷题-1
小A 和 小B 在玩猜数字.小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜.他们一共进行三次这个游戏,请返回 小A 猜对了几次? 输入的guess数组为 小 ...
- SVN安装配置教程
第一步:安装Apache LInux centos6.5 (备注:为了方便可以把linux防火墙关掉,这样就不需要一个一个开端口了,建议开发测试可以这样,正式环境不推荐) 第二步:安装SVN服 ...
- P2921 [USACO08DEC]在农场万圣节[SCC缩点]
题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵 ...
- 50道sql练习题及答案与详细分析
数据表介绍 --1.学生表 Student(SId,Sname,Sage,Ssex) --SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --2.课程表 Course( ...
- java中为什么不能通过getClass().getName()获取父类的类名
例如: class A{} public class B extends A{ public void test(){ System.out.println(super.getClass().getN ...
- 除了不要 SELECT * ,程序员使用数据库还应知道的11个技巧
SQL:sum里加条件SELECT SUM( CASE WHEN "V7010" BETWEEN 0 AND 0.1 THEN 1 ELSE 0 END) FROM "C ...
- MySQL 新建用户并赋予权限
创建一个用户: create user 'oukele'@'%' identified by 'oukele'; 提示下面所列出的信息的话,得刷新一下权限表 The MySQL server is r ...
- 多任务案例--文件夹copy.py
import os import multiprocessing def copy_file(q,file_name,new_folder_name,old_folder_name): with op ...
- mysql中删除重复数据
//首先我们需要知道我们重复的都有哪些数据, //第一步:进行对数据表进行分组,group by. //第二步:进行后通过having进行限制筛选,条数大于等于2的 //第三步:进行多表删除. //案 ...
- Base 编解码(转)
private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0 ...