Lua学习之从文件中找字符串
来源:互联网 发布:电吉他效果器软件ios 编辑:程序博客网 时间:2024/06/06 10:53
Lua学习之从文件中找字符串
涉及简单的文件打开关闭操作,和String库中string.find()的用法。
我们在和此代码所在文件的路径里放了一个文件夹lifeforrent.txt,里面是life for rent 的歌词,我们查找里面”nothing”出现的所在行。
代码块
-- find the string "nothing"function allwords() local line = file:read() local pos = 1 local row = 1 return function () while line do local s, e = string.find(line,"rent",pos) if s then pos = e + 1 return row,string.sub(line,s,e) else line = file:read() pos = 1 row = row + 1 end end return nil endend-- open the filefile = assert(io.open("lifeforrent.txt","r"))for index,word in allwords() do print (index,word)end-- close the filefile:close()
下面是运行结果:
0 0
- Lua学习之从文件中找字符串
- Lua学习笔记三之从Lua中调用C
- 从文件中找矩阵
- 从字符串中找数字 string 中找digits
- Shell在文件中找指定字符串
- 从zip 文件中找文件名含有某些字符串的文件/已经所含文件中含有某个字符串的文件//grep
- 字符串中找字符串
- 纯C语言:从字符串中找整数
- Lua语言学习之字符串库
- 开始学习LUA,在LUA中使用UNICODE字符串
- 从文件中获取字符串
- 从文件中读取字符串
- 字符串中找数字
- VTK观察者和picker学习--从源码中找解决方案
- opencv3学习之从文件/摄像头中读视频
- Lua学习笔记二之C中调用Lua
- cocos2dx lua 读取table 从 cvs文件中
- 从 filedonkey.com 找文件
- MongoDB数据访问[C#]
- java调用Python的方法
- UICollectionview不滚动
- 致佳音: 推箱子游戏自动求解算法设计(一)
- <C/C++图>搜索算法:DFS与BFS
- Lua学习之从文件中找字符串
- 1092. To Buy or Not to Buy (20)建立查询表,或者叫哈希表
- 战国策评析(三)---齐
- C#水晶报表教程
- NYOJ 题目1091 还是01背包(二分,超大01背包)
- Solr4.10.2集成Nutch1.9与自带UI界面使用
- Maven的作用到底是什么?
- read()
- IE9在WIN7下不能输入任何文字数字 字母