多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 字符串,数组,表 - 支持单引号,多引号,\[\["字符串"\]\]三种;\\n换行,\\t制表,\\本身,' "单双引号 ``` <pre class="calibre14">``` arg <span class="token">=</span> string<span class="token2">.</span><span class="token1">upper</span><span class="token2">(</span>arg<span class="token2">)</span> 转换大小 lower小写 reverse反转字符 string<span class="token2">.</span><span class="token1">gsub</span><span class="token2">(</span><span class="token4">"aaaa"</span><span class="token2">,</span><span class="token4">"a"</span><span class="token2">,</span><span class="token4">"z"</span><span class="token2">,</span><span class="token3">3</span><span class="token2">)</span><span class="token2">;</span> 查找a替换成c<span class="token2">,</span>次数<span class="token3">3</span><span class="token2">,</span>忽略次数是全部 string<span class="token2">.</span><span class="token1">find</span><span class="token2">(</span><span class="token4">"Hello Lua user"</span><span class="token2">,</span> <span class="token4">"Lua"</span><span class="token2">)</span><span class="token2">;</span> 查找lua返回位置<span class="token3">7</span><span class="token2">,</span>从<span class="token3">1</span>开始<span class="token2">,</span>不才尊返回nil<span class="token2">,</span>php中空白 string<span class="token2">.</span><span class="token1">format</span><span class="token2">(</span><span class="token4">"the value is:%d"</span><span class="token2">,</span><span class="token3">4</span><span class="token2">)</span> 格式化字符 string<span class="token2">.</span><span class="token1">char</span><span class="token2">(</span><span class="token3">97</span><span class="token2">,</span><span class="token3">98</span><span class="token2">,</span><span class="token3">99</span><span class="token2">,</span><span class="token3">100</span><span class="token2">)</span> 返回abcd string<span class="token2">.</span><span class="token1">byte</span><span class="token2">(</span><span class="token4">"ABCD"</span><span class="token2">,</span><span class="token3">4</span><span class="token2">)</span> <span class="token3">68</span>第四个 不填是第一个 string<span class="token2">.</span><span class="token1">len</span><span class="token2">(</span><span class="token4">"abc"</span><span class="token2">)</span> <span class="token3">3</span> 中文一个是<span class="token3">3</span> string<span class="token2">.</span><span class="token1">rep</span><span class="token2">(</span><span class="token4">"abcd"</span><span class="token2">,</span><span class="token3">2</span><span class="token2">)</span> 返回拷贝数 ``` ``` - 数组下标从1开始 ``` <pre class="calibre14">``` array <span class="token">=</span> <span class="token2">{</span><span class="token4">"Lua"</span><span class="token2">,</span> <span class="token4">"Tutorial"</span><span class="token2">}</span> 一维数组 ``` ``` - 表,删除表mytable = nil ``` <pre class="calibre14">``` tables<span class="token">=</span><span class="token2">{</span><span class="token2">}</span> tables<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span><span class="token">=</span><span class="token4">"aaaa"</span> table<span class="token2">.</span><span class="token1">concat</span><span class="token2">(</span>fruits<span class="token2">,</span><span class="token4">", "</span><span class="token2">,</span> <span class="token3">2</span><span class="token2">,</span><span class="token3">3</span><span class="token2">)</span> 链接字符 table<span class="token2">.</span><span class="token1">insert</span><span class="token2">(</span>fruits<span class="token2">,</span><span class="token4">"mango"</span><span class="token2">)</span> 插入元素<span class="token2">,</span>默认在尾部<span class="token2">,</span>可以指定索引table<span class="token2">.</span><span class="token1">insert</span><span class="token2">(</span>fruits<span class="token2">,</span><span class="token3">2</span><span class="token2">,</span><span class="token4">"mango"</span><span class="token2">)</span> table<span class="token2">.</span><span class="token1">remove</span><span class="token2">(</span>fruits<span class="token2">)</span> 删除最后一个 table<span class="token2">.</span><span class="token1">sort</span><span class="token2">(</span>fruits<span class="token2">)</span> 排序 ``` ``` - 模块,表去实现,require "mod" ``` <pre class="calibre14">``` module <span class="token">=</span> <span class="token2">{</span><span class="token2">}</span> module<span class="token2">.</span>a<span class="token">=</span><span class="token4">"常量"</span> <span class="token5">function</span> module<span class="token2">.</span><span class="token1">func1</span><span class="token2">(</span><span class="token2">)</span> io<span class="token2">.</span><span class="token1">write</span><span class="token2">(</span><span class="token4">"这是一个公有函数!\n"</span><span class="token2">)</span> end local <span class="token5">function</span> <span class="token1">func2</span><span class="token2">(</span><span class="token2">)</span> <span class="token1">print</span><span class="token2">(</span><span class="token4">"这是一个私有函数!"</span><span class="token2">)</span> end <span class="token5">function</span> module<span class="token2">.</span><span class="token1">func3</span><span class="token2">(</span><span class="token2">)</span> <span class="token1">func2</span><span class="token2">(</span><span class="token2">)</span> end <span class="token5">return</span> module ``` ``` - 读取输出文件 ``` <pre class="calibre16">``` file<span class="token">=</span>io<span class="token2">.</span><span class="token1">open</span><span class="token2">(</span><span class="token4">"1.txt"</span><span class="token2">,</span><span class="token4">"a+"</span><span class="token2">)</span> w<span class="token">+</span>会清<span class="token3">0</span> a<span class="token">+</span>是追加 不存在会自动创建文件 io<span class="token2">.</span><span class="token1">input</span><span class="token2">(</span>file<span class="token2">)</span> arg<span class="token">=</span>io<span class="token2">.</span><span class="token1">read</span><span class="token2">(</span><span class="token4">"*a"</span><span class="token2">)</span> io<span class="token2">.</span><span class="token1">output</span><span class="token2">(</span>file<span class="token2">)</span> io<span class="token2">.</span><span class="token1">write</span><span class="token2">(</span><span class="token4">"-- test.lua 文件末尾注释"</span><span class="token2">)</span> io<span class="token2">.</span><span class="token1">close</span><span class="token2">(</span>file<span class="token2">)</span> ``` ```