<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                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 ~~~ 運行結果: ![](https://box.kancloud.cn/2016-02-22_56cb2ca25a77f.jpg) **2.string.gsub** 說明:函數有三個參數:目標串,模式串,替換串。他基本作用是用來查找匹配模式的串,并將使用替換串其替換掉: ~~~ s = string.gsub("Lua is cute", "cute", "great") print(s) -->Lua is great --第四個參數是可選的,用來限制替換的范圍 s = string.gsub("all lii", "l", "x", 1) print(s) -->axl lii _, count = string.gsub("all lii", "l", "x", 2) print(count) -->2 ~~~ 注:_ 只是一個啞元變量 **3.string.gfind?** 說明:遍歷一個字符串內所有匹配模式的子串。如: ~~~ --收集一個字符串中所有的單詞,然后插入到一個表中: local s = "hello hi, again" words = {} for w in string.gfind(s, "(%a)") do table.insert(words, w) end for k, v in pairs(words) do print(k,v) end ~~~ 運行效果: ![](https://box.kancloud.cn/2016-02-22_56cb2ca26a04a.jpg) ## 三、模式 字符類指可以匹配一個特定字符集合內任何字符的模式項。比如,字符類%d匹配任意數字。 ~~~ s = "Deadline is 30/05/1999, firm" date = "%d%d/%d%d/%d%d%d%d" print(string.sub(s, string.find(s, date))) --> 30/05/1999 ~~~ 下面的表列出了Lua支持的所有字符類: ![](https://box.kancloud.cn/2016-02-22_56cb2ca27aaa8.jpg) 在模式匹配中有一些特殊字符,他們有特殊的意義,Lua中的特殊字符如下: ![](https://box.kancloud.cn/2016-02-22_56cb2ca28a1ec.jpg) 例子: ~~~ print(string.gsub("hello, A up-down!", "%A", ".")) -->hello..A.up.down. 5 --參數4不是字符串結果的一部分,他是gsub返回的第二個結果,代表發生替換的次數 --[]方括號將字符類或者字符括起來創建自己的字符類 print(string.gsub("text", "[AEIOUaeiou]", "")) -->txt 1 print(string.len("one, and two; and three")) -->23 print(string.gsub("one123, and two", "%d+", "XXX")) -->oneXXX, and two 1 ~~~ # ## 四、捕獲 Capture3是這樣一種機制:可以使用模式串的一部分匹配目標串的一部分。將你想捕獲的模式用圓括號括起來,就指定了一個capture。 在string.find使用captures的時候,函數會返回捕獲的值作為額外的結果。這常被用來將一個目標串拆分成多個: ~~~ pair = "name = Anna" a, b, key, value = string.find(pair, "(%a+)%s*=%s*(%a+)") print(a," ",b," ",key," ",value) --> 1 11 name Anna ~~~ '%a+' 表示非空的字母序列;'%s*' 表示0個或多個空白。在上面的例子中,整個模式代表:一個字母序列,后面是任意多個空白,然后是 '=' 再后面是任意多個空白,然后是一個字母序列。兩個字母序列都是使用圓括號括起來的子模式,當他們被匹配的時候,他們就會被捕獲。當匹配發生的時候,find函數總是先返回匹配串的索引下標,然后返回子模式匹配的捕獲部分。 我們常常需要使用string.gsub遍歷字符串,而對返回結果不感興趣。比如,我們收集一個字符串中所有的單詞,然后插入到一個表中: ~~~ --收集一個字符串中所有的單詞,然后插入到一個表中: local s = "hello hi, again" words = {} for w in string.gfind(s, "(%a)") do table.insert(words, w) end for k, v in pairs(words) do print(k,"->",v) end ~~~ 運行結果: ![](https://box.kancloud.cn/2016-02-22_56cb2ca29b41c.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看