IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

三十四、Lua string库

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

文章永久连接:https://tech.souyunku.com/?p=8602

Lua解释器对字符串的支持很有限。一个程序可以创建字符串并连接字符串,但不能截取子串,检查字符串的大小,检测字符串的内容。在Lua中操纵字符串的功能基本来自于string库。

一、String库的常用函数:

--返回字符串s的长度
local s = "HelloWorld"
print(string.len(s))  -->10

--重复n次字符串s的串
print(string.rep(s,2))  -->HelloWorldHelloWorld

--大写字母转换成小写
print(string.lower(s))  -->helloworld

--小写转换成大写
print(string.upper(s))  -->HELLOWORLD

--截取字符串
local s = "[in brackets]"
print(string.sub(s,2,-1))  -->in brackets]

--将每一个数字转换成字符
print(string.char(97))  -->a

--将每一个字符转换成数字
print(string.byte("abc"))
print(string.byte("abc", 2)) --> 98
print(string.byte("abc", -1)) --> 99

--注:使用负数索引访问字符串的最后一个字符

--对字符串进行格式化输出
PI = 3.14165120
print(string.format("pi = %.4f", PI))  -->pi = 3.1417
--注释:使用和C语言的printf函数几乎一模一样,你完全可以照C语言的printf来使用这个函数.

注:

string库中所有的字符索引从前往后是1,2,…;从后往前是-1,-2,…

string库中所有的function都不会直接操作字符串,而是返回一个结果。

二、String库的模式匹配函数

在string库中功能最强大的函数是:string.find(字符串查找),string.gsub(全局字符串替换),and string.gfind(全局字符串查找)。这些函数都是基于模式匹配的。

1、string.find

说明:用来在目标串(subject string)内搜索匹配指定的模式的串。函数如果找到匹配的串返回他的位置,否则返回nil.最简单的模式就是一个单词,仅仅匹配单词本身。比如,模式’hello’仅仅匹配目标串中的”hello”。当查找到模式的时候,函数返回两个值:匹配串开始索引和结束索引。

local s = "hello world"
i,j = string.find(s,"hello")
print(i,"   ",j)  -->1      5

print(string.find(s, "kity"))  -->nil

string.find函数第三个参数是可选的:标示目标串中搜索的起始位置。当我们想查找目标串中所有匹配的子串的时候,这个选项非常有用。

local s = "\nare you ok!\n OK\n"
local t = {}
local i = 0
while true do
    i = string.find(s,"\n",i+1)
    if i == nil then break end
    table.insert(t,i)   
end

for k,v in pairs(t) do
    print(k,"->",v)  
end

运行结果:

干货推荐


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(95) 打赏



未经允许不得转载:搜云库技术团队 » 三十四、Lua string库

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367