Cocos2d-Lua之string
来源:互联网 发布:php万能参数 编辑:程序博客网 时间:2024/05/14 11:51
一、string的查找操作
格式:
_start, _end = string.find(字符串, 要查找的字符串, 起始查找位置) //若缺省起始查找位置,则从第一个位置开始查找,并返回查找到的起始位置和结束位置
function strfind() local a = "helloworldhelloworld" local i = 0 repeat _start, _end = string.find(a, "ld", i) if(_start ~= nil) then print(_start .. "--" .. _end) i = _end else break end until falseend
string.find一次只能对要查找的字符串定位一次,若字符串中出现多次该要查找的字符串,就要用到循环。
输出结果:
这样便找到了字符串中该查找字符串所有出现的位置
二、string的替换
格式:
str = string.gsub(字符串, 要被替换的字符串, 替换字符串) //返回一个替换后的字符串
function strreplace() a = "aaabbbccc" a = string.gsub(a, "a", "dd") print(a)end
输出结果:
三、输入多行字符串
使用“”双引号是不能让一个字符串包含多行内容的,这里就要用到[[]]符号。
function manyline() str = [[ <html> <title></title> <body> hello world </body> </thml> ]] print(str)end
输出结果:
这里若果用双引号就会报错,因为双引号不支持多行字符串。
四、number转string类型
格式:
str = tostring(number) //返回一个string类型的值
function num2str() a = 15 print(type(a)) a = tostring(a) print(type(a))end
输出结果:
可以看到number类型的变量a转换成了string类型。
五、string倒置
格式:
str = string.reverse(str) //返回倒置后的string类型值
function strreverse() s = "hello" s = string.reverse(s) print(s)end
输出结果:
0 0
- Cocos2d-Lua之string
- Cocos2d-Lua之函数
- Cocos2d-Lua之赋值
- Cocos2d-Lua之table
- Cocos2d-Lua之随机数
- Cocos2d-x 之Lua初学
- Cocos2d-X之LUA笔记
- Cocos2d-Lua之运算符
- Cocos2d-Lua之循环语句
- Cocos2d-Lua之math库
- Cocos2d-Lua之文件操作
- Cocos2d-Lua之异常处理
- Cocos2d-Lua之协同程序
- Cocos2d-Lua之闭包
- LUA之string的使用
- lua之string库详解
- Lua笔记之string详解
- LUA之string的使用
- SIP 简介
- xCode正则表达式替换查找
- 虚拟机安装centos6.5出现 unsupported hardware detected 解决方法
- Android 使用android-support-multidex解决Dex超出方法数的限制问题,让你的应用不再爆棚
- STL源码剖析——multiset
- Cocos2d-Lua之string
- 文本框的测试
- java根据ftl模板生成word文档
- 安卓压缩图片
- BT5网卡配置
- Python脚本文件转为exe文件---py2exe
- IEEE 802.15.4协议完整中文版(6)
- 【IOS网络编程】socket编程 - Asyncsocket
- STL源码剖析——multimap