規則。細繩
表示字串值的基本型別。
可以使用==
、 !=
、 >
、 <
、 >=
和<=
運算子以字典順序比較字串。
可以使用+
運算子連接字串:
// Concatenate a username and an email domain 'username' + '@domain.com'
可以使用索引運算子[]
存取子字串。也可以使用範圍運算子[i:j]
來存取它們。請注意,參數j
(範圍運算子中的上限)不包含在內。
// Check if the first character of a string is 'a' mystring[0] == 'a' // Check if the string starts with 'abc' mystring[0:3] == 'abc'
布林值、整數、浮點數和空值可以使用string()
函數轉換為字串:
string(true) == "true" string(1) == "1" string(2.0) == "2.0" string(null) == "null"
方法
降低
lower() 傳回規則.String
傳回輸入字串的小寫版本。
- 退貨
non-null rules.String
小寫字串。
例子
'ABC'.lower() == 'abc'
'ABC123'.lower() == 'abc123'
火柴
matches(re) 回傳規則.Boolean
對整個字串執行正規表示式比對。
範圍 | |
---|---|
關於 | 使用Google RE2 語法的正規表示式。 值不能為空。 |
- 退貨
non-null rules.Boolean
如果整個字串符合則為 true,否則為 false。
例子
'user@domain.com'.matches('.*@domain[.]com') == true
'banana'.matches('.*@domain[.]com') == false
代替
替換(re,sub)返回rules.String
將與正規表示式相符的所有子字串替換為使用者提供的字串。
範圍 | |
---|---|
關於 | 使用Google RE2 語法的正規表示式。 值不能為空。 |
子 | 要替換的字串。 值不能為空。 |
- 退貨
non-null rules.String
表示替換操作結果的字串。如果沒有子字串與正規表示式匹配,則傳回未修改的原始字串。
例子
'banana'.replace("a", "o") == 'bonono'
'banana'.replace("ana", "ee") == 'beena'
'foo@test.com'.replace(".", "-") == '---------------' // '.' regex match all
尺寸
size() 傳回規則.Integer
傳回字串中的字元數。
- 退貨
non-null rules.Integer
元符數。
例子
'a'.size() == 1
'abc'.size() == 3
分裂
split(re) 傳回規則。列表
根據正規表示式分割字串。
範圍 | |
---|---|
關於 | 使用Google RE2 語法的正規表示式。 值不能為空。 |
- 退貨
non-null rules.List
字串清單。
例子
'a/b/c'.split('/') == ['a', 'b', 'c']
轉UTF8
toUtf8() 回傳規則.位元組
傳回字串的 UTF-8 位元組編碼。
- 退貨
non-null rules.Bytes
包含字串的 UTF-8 編碼表示形式的位元組序列。
例子
'**'.toUtf8() == b'\x2A\x2A'
'€'.toUtf8() == b'\xE2\x82\xAC'
修剪
Trim() 傳回規則.String
傳回刪除了前導空格和尾隨空格的字串版本。
- 退貨
non-null rules.String
字串修剪後的字串。
例子
' a '.trim() == 'a'
'b'.trim() == 'b'
上
upper() 傳回規則.String
傳回輸入字串的大寫版本。
- 退貨
non-null rules.String
大寫字串。
例子
'abc'.upper() == 'ABC'
'abc123'.upper() == 'ABC123'