字串資料型別是一系列字元,如字母、數字和符號。這是用來儲存大多數基於文字的信息的資料型別。
聲明字串
要聲明一個字串變數,將字元放在引號中。通常使用雙引號("),但單引號(')也可以。如果你想在字串中包含單引號或雙引號,可以將字串放在另一種類型的引號中,或者使用轉義引號。
local string1 = "Hello world!"print(string1) --> Hello world!local string2 = 'Hello "world"!'print(string2) --> Hello "world"!
要在字串中包括單引號和雙引號,或者創建多行字串,使用雙方括號進行聲明:
local string1 = [[Helloworld!Hello "world"!Hello 'world'!]]print(string1)--> Hello--> world!--> Hello "world"!--> Hello 'world'!
如果需要,可以在字串中使用相同數量的等號來嵌套多個方括號:
local string1 = [=[Hello[[world!]]]=]print(string1)--> Hello--> [[world!]]
合併字串
要合併字串,使用兩個點(..)進行串聯。串聯字串不會在它們之間插入空格,因此你需要在前一個/下一個字串的結尾/開頭包含空格,或者在兩個字串之間串聯一個空格。
local hello = "Hello"local helloWithSpace = "Hello "local world = "world!"local string1 = hello .. worldlocal string2 = helloWithSpace .. worldlocal string3 = hello .. " " .. worldprint(string1) --> Helloworld!print(string2) --> Hello world!print(string3) --> Hello world!
請注意,print()命令接受多個參數並以空格進行合併,因此你可以使用 , 代替 .. 來在 print() 輸出中產生空格。
local hello = "Hello"local world = "world"local exclamationMark = "!"print(hello .. world .. exclamationMark) --> Helloworld!print(hello, world .. exclamationMark) --> Hello world!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) --> Hello 'world'!local string2 = "Hello \"world\"!"print(string2) --> Hello "world"!
某些字元在反斜杠後會產生特殊字元,而不是轉義字元:
- 嵌入換行,使用 \n。
- 嵌入水平製表,使用 \t。
local string1 = "Hello\nworld!"print(string1)--> Hello--> world!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) --> Hello world, 1 time!print(string2) --> Hello world, 2 times!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: 嘗試對字串和數字進行算術運算 (加)
比較
字串可以使用 <、<=、> 和 >= 運算符進行比較,這些運算符是基於字串中每個字元的 ASCII 碼進行字典序比較的。 這將導致以字串形式表示的數字無法正確比較,例如,"100" 將小於 "20",因為位元組 "0" 和 "1" 的 ASCII 碼低於位元組 "2"。
print("Apple" < "apple") --> trueprint("Banana" < "apple") --> true (B 在 ASCII 中位於 a 之前)print("number100" < "number20") --> true
字串模式參考
字串模式是可以與 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 | 字母數字字元(字母或數字) | aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789 |
| %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") -- 匹配,因為 "first" 在開頭print(start1) --> firstlocal start2 = string.match("third second first", "^first") -- 不匹配,因為 "first" 不在開頭print(start2) --> nillocal end1 = string.match("first second third", "third$") -- 匹配,因為 "third" 在結尾print(end1) --> thirdlocal end2 = string.match("third second first", "third$") -- 不匹配,因為 "third" 不在結尾print(end2) --> nil
你也可以同時使用 ^ 和 $ 來確保模式僅與整個字串匹配,而不僅僅是某部分。
-- 使用 ^ 和 $ 來匹配整個字串local match1 = string.match("Roblox", "^Roblox$") -- 匹配,因為 "Roblox" 是整個字串(相等)print(match1) --> Robloxlocal match2 = string.match("I play Roblox", "^Roblox$") -- 不匹配,因為 "Roblox" 不在開頭和結尾print(match2) --> nillocal match3 = string.match("I play Roblox", "Roblox") -- 匹配,因為 "Roblox" 包含在 "I play Roblox" 中print(match3) --> Roblox
類別修飾符
僅憑字元類別,會在字串中僅匹配 一個 字元。例如,以下模式("%d")從左到右閱讀字串,找到 第一個 數字(2),然後停止。
local match = string.match("The Cloud Kingdom has 25 power gems", "%d")print(match) --> 2
你可以對任何字元類別使用 修飾符 來控制結果:
| 計數詞 | 含義 |
|---|---|
| + | 匹配 1 或多個前一個字元類別 |
| - | 儘量匹配最少的前一個字元類別 |
| * | 匹配 0 或多個前一個字元類別 |
| ? | 匹配 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
類別集合
集合 應用於單個字元類別無法完成整個任務的情況。例如,您可能想要通過單一模式匹配小寫字母(%l)和標點符號字母(%p)。
集合由括號 [] 圍繞定義。在以下範例中,注意使用集合("[%l%p]+")與不使用集合("%l%p+")之間的區別。
local match1 = string.match("Hello!!! I am another string.", "[%l%p]+") -- 集合print(match1) --> ello!!!local match2 = string.match("Hello!!! I am another string.", "%l%p+") -- 非集合print(match2) --> o!!!
第一個命令(集合)告訴 Luau 查找小寫字母和標點符號。將 + 計數詞添加到整個集合之後,它找到所有這些字元(ello!!!),並在到達空格時停止。
在第二個命令(非集合)中,+ 計數詞僅適用於前面的 %p 類,因此 Luau 僅抓取第一個小寫字母(o)再次標點(!!!)。
如同字元類別,集合也可以是自己的“對立面”。這通過在 [ 後立即添加 ^ 字元來完成。例如,"[%p%s]+" 代表標點符號和空格,而 "[^%p%s]+" 代表所有 除了 標點符號和空格的字元。
集合還支援 範圍,讓你在起始字元和結束字元之間查找整個範圍的匹配。這是一個較高級的功能,詳情請參見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) --> TwoThousand 2000local key3, val3 = string.match("OneMillion=1000000", pattern)print(key3, val3) --> OneMillion 1000000
跟在 %s 類別後面的 ? 計數詞是安全的,因為它使等於符號兩側的空格成為可選。如果在等號周圍缺少一個(或兩個)空格,則匹配成功。
在 string.gsub() 函數中,您可以通過使用 %1、%2、%3 等包含捕獲組來創建替換字串,最多可達 %9。儘管 Luau 支援最多 32 個捕獲組,超過此數量將拋出錯誤,但您只能使用此語法引用前九個:
local str = "love2play Roblox"local pattern = "(%w+)(%d+)(%w+)%s+(%w+)"local replacement = "I %1 %2 %3 %4!"local result = string.gsub(str, pattern, replacement)print(result) --> I love 2 play Roblox!
您也可以嵌套字串捕獲:
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--> The Cloud Kingdom is heavenly--> Cloud Kingdom--> The Forest Kingdom is peaceful--> Forest Kingdom
這個模式搜索的工作方式如下:
string.gmatch() 迭代器查找整個由外層括號定義的 "description" 模式的匹配。這在第一个逗號停下來並捕獲:
| # | 模式 | 捕獲 |
|---|---|---|
| 1 | (The%s(%a+%sKingdom)[%w%s]+) | The Cloud Kingdom is heavenly |
使用其成功的第一個捕獲,迭代器然後會尋找與內層括號定義的 "kingdom" 模式的匹配。這個嵌套模式單純地捕獲:
| # | 模式 | 捕獲 |
|---|---|---|
| 2 | (%a+%sKingdom) | Cloud Kingdom |
然後,迭代器回過頭來繼續在完整字符串中搜索,捕獲以下內容:
| # | 模式 | 捕獲 |
|---|---|---|
| 3 | (The%s(%a+%sKingdom)[%w%s]+) | The Forest Kingdom is peaceful |
| 4 | (%a+%sKingdom) | Forest Kingdom |
除了以上所有內容,還有一個特殊情況是 空捕獲(())。如果捕獲是空的,則將捕獲字串中的位置:
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--> Cloud 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