字串 資料類型是一個字元列表,例如字母、數字和符號。它是儲存大多數文字資訊的數據類型。
宣言字串
要宣言字串變量,將引號放在字符周圍。使用雙引號(")比較常見,但單引號(')也可以使用。如果您想在字串中包含單或雙引號,將字串包圍在另一種引號類型上,或使用 逃出的引號 。
local string1 = "Hello world!"print(string1) --> 你好,世界!local string2 = 'Hello "world"!'print(string2) --> Hello "world"!
要將單一和雙重引言包含在字串中,或要創建多行字串,請使用雙重括號來宣言:
local string1 = [[Helloworld!Hello "world"!Hello 'world'!]]print(string1)--> 你好--> 世界!--> 你好「世界」!--> Hello 'world'!
如有需要,您可以使用相同數量的等號標記在開始和結束括號內巢穴多個括號:
local string1 = [=[Hello[[world!]]]=]print(string1)--> 你好--> [[world!]]
結合字串
要結合字串, 將它們連接 成兩點(..)。連接字串不會在其間插入空格,因此您需要在前一個/後一個字串的結尾或開頭包含空空間(s),或在兩個字串之間連接空格。
local hello = "Hello"local helloWithSpace = "Hello "local world = "world!"local string1 = hello .. worldlocal string2 = helloWithSpace .. worldlocal string3 = hello .. " " .. worldprint(string1) --> 你好世界!print(string2) --> 你好,世界!print(string3) --> Hello world!
請注意,print() 命令需要多個參數並將它們結合 與 空格,因此您可以使用, 而不是..來產生空格在print()輸出。
local hello = "Hello"local world = "world"local exclamationMark = "!"print(hello .. world .. exclamationMark) --> 你好世界!print(hello, world .. exclamationMark) --> 你好,世界!print(hello, world, exclamationMark) --> Hello world !
轉換字串
要將字串轉換為數字,請使用 tonumber() 函數。如果字串沒有數字表示,tonumber() 將返回 nil。
local numericString = "123"print(tonumber(numericString)) --> 123local alphanumericString = "Hello123"print(tonumber(alphanumericString)) --> nil
逃離字串
要逃離雙引號或單引號字串宣言並嵌入幾乎任何字符,請在字符前加入撇號 ( \ )。例如:
- 要將單一引言嵌入單一引言字串,請使用 \' 。
- 要將雙引號嵌入到雙引號字串中,請使用 \" 。
local string1 = 'Hello \'world\'!'print(string1) --> 你好,「世界」!local string2 = "Hello \"world\"!"print(string2) --> Hello "world"!
某些字符跟隨斜杆產生的特殊字符而不是逃出的字符:
- 要插入新行,請使用 \n 。
- 要嵌入橫向標籤,請使用 \t 。
local string1 = "Hello\nworld!"print(string1)--> 你好--> 世界!local string2 = "Hello\tworld!"print(string2) --> Hello world!
字串插入
Luau 支持 字串插入 ,一項允許您將式子插入字串的功能。使用撇號 ( ` ) 來宣言插入的字串,然後添加在括號內的式子:
local world = "world"local string1 = `Hello {world}!`print(string1) --> Hello world!
雖然變量是最常用的,但您可以使用任何式子,包括數學:
local world = "world"local number = 1local letters = {"w", "o", "r", "l", "d"}local string1 = `Hello {world}, {number} time!`local string2 = `Hello {world}, {number + 1} times!`local string3 = `Hello {table.concat(letters)} a third time!`print(string1) --> 你好世界,1次!print(string2) --> 你好世界,2次!print(string3) --> Hello world a third time!
標準逃逸規則適用於引號、捲髮括號和撇號:
local string1 = `Hello \`\{world\}\`!`print(string1) --> Hello `{world}`!
數學轉換
如果您對字串進行數學運算,Luau 會自動將字串轉換為數字。如果字串沒有數字表示,它會發出錯誤。
print("55" + 10) --> 65print("55" - 10) --> 45print("55" * 10) --> 550print("55" / 10) --> 5.5print("55" % 10) --> 5print("Hello" + 10) --> print("Hello" + 10):1: attempt to perform arithmetic (add) on string and number
比較
字串可以使用 <、<=、> 和 >= 運算符進行比較,這些運算符會根據字串中每個字元的ASCII代碼進行比較,以比較字串。這會導致在字串中的數字無法正確比較,例如, "100" 將小於 "20" ,因為 bytes "0" 和 bytes "1" 的 ASCII 代碼比 byte "2" 低。
print("Apple" < "apple") --> 真print("Banana" < "apple") --> 真 (B 在 ASCII 前)print("number100" < "number20") --> true
字串模式參考
A 字串模式 是您可以使用 string.match()、string.gmatch() 和其他函數來尋找更長字串的一部分或子字串的組合。
直接匹配
您可以在 Luau 函數中使用直接匹配,例如 string.match() , 除了 魔法字符 。例如,這些指令尋找字串內的 Roblox 字詞:
local match1 = string.match("Welcome to Roblox!", "Roblox")local match2 = string.match("Welcome to my awesome game!", "Roblox")print(match1) --> Robloxprint(match2) --> nil
角色類別
角色類別對於更進階的字串搜尋至關重要。您可以使用它們來搜索不一定是字元特定的內容,但屬於已知類別(類)的內容,包括 信件 , 數字 , 空格 , 句點符號 和更多。
下表顯示 Luau 字串模式的官方角色類別:
類別 | 代表 | 範例匹配 |
---|---|---|
. | 任何字符 | 32kasGJ1%fTlk?@94 |
%a | 大寫或小寫的字母 | aBcDeFgHiJkLmNoPqRsTuVwXyZ |
%l | 小寫字母 | abcdefghijklmnopqrstuvwxyz |
%u | 大寫字母 | ABCDEFGHIJKLMNOPQRSTUVWXYZ |
%d | 任何數字 (數字) | 0123456789 |
%p | 任何點號字符 | !@#;,. |
%w | 一個字母數字字元(是字母 或 數字) | abcdefghijklmnopqrsztuvwxyz0123456789 |
%s | 空格或白色空格字元 | , \n , 和 \r |
%c | 特殊的 控制字符 | |
%x | 十六進位字元 | 0123456789ABCDEF |
%z | NULL 字元 ( \0 ) |
對於單字類別,例如 %a 和 %s,對應的大寫字母代表類別的「反對」。例個體、實例,%p 代表一個句點符號,而 %P 代表所有字符,除了句點符號。
魔法字符
有 12 個「魔法字元」被保留在模式中用於特殊用途:
$ | % | ^ | * | ( | ) |
. | [ | ] | + | - | ? |
您可以使用 % 符號逃脫並搜尋魔法字元。例如,要搜尋 roblox.com ,請先以 . (時間) 符號前置,將其與 % (時間) 符號結合,如在 %. 中。
-- 「roblox.com」與「roblox#com」匹配,因為期間被解釋為「任何字元」local match1 = string.match("What is roblox#com?", "roblox.com")print(match1) --> roblox#com-- 使用 % 逃離期間,以便被解釋為字面期間字元local match2 = string.match("I love roblox.com!", "roblox%.com")print(match2) --> roblox.com
錨定
您可以使用 ^ 和 $ 符號在字串的開頭或結尾搜尋模式。
local start1 = string.match("first second third", "^first") -- 匹配因為「第一」在開頭print(start1) --> 第一local start2 = string.match("third second first", "^first") -- 不匹配,因為「第一」不在開頭print(start2) --> 零local end1 = string.match("first second third", "third$") -- 因為「第三」在結結束,所以匹配print(end1) --> 第三local end2 = string.match("third second first", "third$") -- 不匹配,因為 "第三" 不在結結束print(end2) --> nil
您也可以使用 ^ 和 $ 一起來確保模式只匹配完整字串,而不是某些部分字串。
-- 使用 ^ 和 $ 來匹配整個字串local match1 = string.match("Roblox", "^Roblox$") -- 匹配因為「Roblox」是整個字串(平等)print(match1) --> Robloxlocal match2 = string.match("I play Roblox", "^Roblox$") -- 不匹配,因為「Roblox」不在開頭和結結束print(match2) --> 零local match3 = string.match("I play Roblox", "Roblox") -- 因為「Roblox」包含在「我玩 Roblox」內,所以匹配到「Roblox」print(match3) --> Roblox
類別修改器
獨立地,角色類只匹配 一 個字符在字串中。例個體、實例,下列模式("%d")從左到右開始閱讀字串,找到 第一個 數字(2),然後停止。
local match = string.match("The Cloud Kingdom has 25 power gems", "%d")print(match) --> 2
您可以使用 修改器 與任何角色類別來控制結果:
數量計 | 意義 |
---|---|
+ | 與前一個字元類別匹配 1 或更多 |
- | 盡可能匹配少量以前的字元類別 |
* | 與前一個字元類別匹配 0 或更多 |
? | 與前一個字元類別匹配 1 或少於 1 個字元 |
%n | 對於 n 介於 1 和 9 之間,匹配等於 n 捕獲字串的子串。 |
%bxy | 平衡捕捉匹配 x , y , 以及之間的所有內容 (例如, %b() 匹配一對括號和之間的所有內容) |
將修改器添加到同一模式("%d+" 而不是 "%d"),輸出 25 而不是 2:
local match1 = string.match("The Cloud Kingdom has 25 power gems", "%d")print(match1) --> 2local match2 = string.match("The Cloud Kingdom has 25 power gems", "%d+")print(match2) --> 25
類別集
集合 應該在單一角色類別無法完成整個工作時使用。例個體、實例,您可能想使用單一模式匹配兩個小寫字母()和句點符號()來匹配單詞。
集合由包括括號 [] 的環繞它們的集合定義。在下面的例子中,注意使用集合( )和不使用集合( )之間的差異。
local match1 = string.match("Hello!!! I am another string.", "[%l%p]+") -- 設定print(match1) --> 你好!!!local match2 = string.match("Hello!!! I am another string.", "%l%p+") -- 未設置print(match2) --> o!!!
第一個指令(設設定)告訴 Luau 尋找小寫字母和句點符號。在整個集設定之後添加了 量化器,它找到了所有那些字符 ( ),當它達到空間格時停止。
在第二個指令(非設置)中,+量化器只適用於前面的%p類,所以Luau只抓住第一個小寫字母(o)之前的連環符號(!!!)。
像角色類別一樣,集合可以是自己的「對立面」。這可以通過在設定合的開頭添加 ^ 字符來實現,直接在打開 [ 之後。例個體、實例, 代表句點和空格,而 代表所有字符,除了句點和空格外。
集合也支持 範圍 ,可以讓您找到從開始和結束字符之間的整個匹配範圍。這是一個高級功能,在 Lua 5.1 手冊 中詳細說明。
字串捕捉
字串 捕捉 是模式內的子模式。這些被括號 () 包圍,用於獲得(擷取)匹配的子串並將它們儲存到變量中。例如,下列模式包含兩個捕捉,(%a+) 和 (%d+),這些捕捉會在相符配成功時返回兩個子字串。
local pattern = "(%a+)%s?=%s?(%d+)"local key1, val1 = string.match("TwentyOne = 21", pattern)print(key1, val1) --> TwentyOne 21local key2, val2 = string.match("TwoThousand= 2000", pattern)print(key2, val2) --> 二千 2000local key3, val3 = string.match("OneMillion=1000000", pattern)print(key3, val3) --> OneMillion 1000000
在以前的模式中,跟隨兩個 %s 類別的 ? 數量符號是安全的添加,因為它使兩側的 = 符號標記變得可選。這意味著如果有一個(或兩個)空格位置缺少在等號標記周圍,匹配成功。
字串捕捉也可以 巢穴化 如下示例:
local places = "The Cloud Kingdom is heavenly, The Forest Kingdom is peaceful"local pattern = "(The%s(%a+%sKingdom)[%w%s]+)"for description, kingdom in string.gmatch(places, pattern) doprint(description)print(kingdom)end--> 雲端王國是天堂--> 雲端王國--> 森林王國是平靜的--> Forest Kingdom
這個模式搜尋的工作方式如下:
迭代器 string.gmatch() 尋找外括號組的父輩定義的整個"說明"模式上的匹配。這在第一個週期停止,並捕捉以追蹤中內容:
# | 模式 | 捕獲 |
---|---|---|
1 | (The%s(%a+%s王國)[%w%s]+) | 雲端王國是天堂 |
使用其成功的第一次擷取,迭代器然後尋找由內部括號組定義的「王國」模式上的匹配。這個嵌套模式簡單地捕捉以追蹤中內容:
# | 模式 | 捕獲 |
---|---|---|
2 | (%a+%s王國) | 雲端王國 |
迭代器然後退出並繼續搜索全字串,捕捉以追蹤中內容:
# | 模式 | 捕獲 |
---|---|---|
3 | (The%s(%a+%s王國)[%w%s]+) | 森林王國是平靜的 |
4 | (%a+%s王國) | 森林王國 |
除了上述所有內容外,還有一個特殊情況,即有一個 空捕捉 ( () )。如果捕捉是空的,則字串中的位置將被捕捉:
local match1 = "Where does the capture happen? Who knows!"local match2 = "This string is longer than the first one. Where does the capture happen? Who knows?!"local pattern = "()Where does the capture happen%? Who knows!()"local start1, finish1 = string.match(match1, pattern)print(start1, finish1) --> 1 42local start2, finish2 = string.match(match2, pattern)print(start2, finish2) --> 43 84
這些特殊捕捉可以像普通捕捉一樣套層:
local places = "The Cloud Kingdom is heavenly, The Forest Kingdom is peaceful."local pattern = "The (%a+()) Kingdom is %a+"for kingdom, position in string.gmatch(places, pattern) doprint(kingdom, position)end--> 雲端 10--> Forest 42
返回的值不尋常,因為它們是 數字 而不是字串:
local match = "This is an example"local pattern = "This is an ()example"local position = string.match(match, pattern)print(typeof(position)) --> number