字串 (str)

所有 字串 都有以下方法。字串是不可變的,所有操作都會將結果以新字串的形式返回。

由以下項目返回

字串物件由以下函數和方法返回

字串方法

str.contains()

如果字串包含指定為參數的字串,則返回 true

簽章

# Returns `true` if string contains the string specified as the argument
bool contains(
  str fragment,     # The string fragment to check
)

範例

target = 'x86_FreeBSD'
is_fbsd = target.to_lower().contains('freebsd')
# is_fbsd now has the boolean value 'true'

參數

方法 str.contains() 接受以下位置參數

名稱 類型 描述 標籤
片段 字串

要檢查的字串片段


str.endswith()

如果字串以指定為參數的字串結尾,則返回 true。

簽章

# Returns true if string ends with the string specified as the argument
bool endswith(
  str fragment,     # The string fragment to check
)

範例

target = 'x86_FreeBSD'
is_bsd = target.to_lower().endswith('bsd') # boolean value 'true'

參數

方法 str.endswith() 接受以下位置參數

名稱 類型 描述 標籤
片段 字串

要檢查的字串片段


str.format()

字串可以使用字串格式化功能來建構。

請參閱 Meson 語法條目 以獲取更多資訊。

自 1.3.0 版本起,除了字串、整數、布林值、選項、字典及其列表之外的值已被棄用。它們先前會印出原始 Python 物件的內部表示形式。

簽章

# Strings can be built using the string formatting functionality
str format(
  str              fmt,       # The string to format
  int | bool | str value...,  # The values to replace the @number@ placeholders in the format string
)

範例

template = 'string: @0@, number: @1@, bool: @2@'
res = template.format('text', 1, true)
# res now has value 'string: text, number: 1, bool: true'

參數

引數扁平化 此函數不支援

方法 str.format() 接受以下位置參數

名稱 類型 描述 標籤
fmt 字串

要格式化的字串。

格式化透過將 @number@ 類型的佔位符替換為相應的 varargs 來工作。

此外,此方法接受 0無限 個可變引數 (value...),類型為 int | bool | str

用於替換格式字串中 @number@ 佔位符的值。


str.join()

與 split 相反,例如 '.'.join(['a', 'b', 'c'] 會產生 'a.b.c'

簽章

# The opposite of split,
str join(
  str strings...,  # The strings to join with the current string
)

範例

# Similar to the Python str.join()
output = ' '.join(['foo', 'bar'])
# Output value is 'foo bar'
pathsep = ':'
path = pathsep.join(['/usr/bin', '/bin', '/usr/local/bin'])
# path now has the value '/usr/bin:/bin:/usr/local/bin'

參數

此方法接受 0無限 個可變引數 (strings...),類型為 str

要使用目前字串連接的字串。

在 Meson 0.60.0 之前,此函數僅接受一個類型為 [[list[str]]] 的單一位置引數。

(自 0.60.0 起)


str.replace()

搜尋所有出現的 old 並將其替換為 new

簽章

(自 0.58.0 起)

# Search all occurrences of `old` and replace it with `new`
str replace(
  str old,     # The substring to search
  str new,     # The replacement string
)

範例

# Replaces all instances of one substring with another
s = 'semicolons;as;separators'
s = s.replace('as', 'are')
# 's' now has the value of 'semicolons;are;separators'

參數

方法 str.replace() 接受以下位置參數

名稱 類型 描述 標籤
old 字串

要搜尋的子字串

new 字串

替換字串


str.split()

在指定字元處 (如果未設定,則為空白字元) 分割字串,並以陣列形式返回各個部分。

簽章

# Splits the string at the specified character
list[str] split(
  str [split_string],   # Specifies the character / substring where to split the string
)

範例

# Similar to the Python str.split()
components = 'a b   c d '.split()
# components now has the value ['a', 'b', 'c', 'd']
components = 'a b   c d '.split(' ')
# components now has the value ['a', 'b', '', '', 'c', 'd', '']

參數

方法 str.split() 接受以下位置參數

名稱 類型 描述 標籤
split_string 字串

指定要分割字串的字元/子字串。

[選用]


str.splitlines()

將字串分割成多行陣列。與 .split('\n') 不同,空字串會產生空陣列,如果字串以換行符號結尾,splitlines() 不會分割最後一個換行符號。'\n'、'\r' 和 '\r\n' 都會被視為換行符號。

簽章

(自 1.2.0 起)

list[str] splitlines()

範例

output = 'hello\nworld\n'.splitlines()
# Output value is ['hello', 'world']
output = ''.splitlines()
# Output value is []
fs = import('fs')
paths = fs.read('my_paths.list').splitlines()
# paths is now the paths listed in 'my_paths.list', or an empty list
# if 'my_paths.list' is empty


str.startswith()

如果字串以指定為參數的字串開頭,則返回 true。

簽章

# Returns true if string starts with the string specified as the argument
bool startswith(
  str fragment,     # The string fragment to check
)

範例

target = 'x86_FreeBSD'
is_x86 = target.startswith('x86') # boolean value 'true'

參數

方法 str.startswith() 接受以下位置參數

名稱 類型 描述 標籤
片段 字串

要檢查的字串片段


str.strip()

移除字串開頭/結尾的字元。

預設情況下,要移除的字元為空格和換行符號。

簽章

# Removes leading/ending characters from the string
str strip(
  str [strip_chars],   # Instead of whitespace, strip all the characters in this string
)

範例

# Similar to the Python str.strip(). Removes leading/ending spaces and newlines
define = ' -Dsomedefine '
stripped_define = define.strip()
# 'stripped_define' now has the value '-Dsomedefine'

參數

方法 str.strip() 接受以下位置參數

名稱 類型 描述 標籤
strip_chars 字串

不要移除空白字元,而是移除此字串中的所有字元。

(自 0.43.0 起)

[選用]


str.substring()

返回從 startend 指定的子字串。startend 引數都是選用的,因此,例如 'foobar'.substring() 將返回 'foobar'

此方法接受負值,其中負數的 start 相對於字串結尾 len(string) - start,負數的 end 也是如此。

如果 startend 超出範圍,則會使用最接近字元的位置。如果 start 大於 end,結果將會是空的子字串。

簽章

(自 0.56.0 起)

# Returns a substring specified from `start` to `end`
str substring(
  int [start],   # The start position
  int [end],     # The end position
)

範例

# Similar to the Python str[start:end] syntax
target = 'x86_FreeBSD'
platform = target.substring(0, 3) # prefix string value 'x86'
system = target.substring(4) # suffix string value 'FreeBSD'

負數值範例

string = 'foobar'
string.substring(-5, -3) # => 'oo'
string.substring(1, -1) # => 'ooba'

超出範圍的值範例

string = 'foobar'
string.substring(64) # => ''
string.substring(0, 64) # => 'foobar'
string.substring(64, 0) # => ''

參數

方法 str.substring() 接受以下位置參數

名稱 類型 描述 標籤
start int

起始位置

[選用]

end int

結束位置

[選用]


str.to_int()

將字串轉換為整數,如果無法轉換則拋出錯誤

簽章

int to_int()

範例

version = '1'
# Converts the string to an int and throws an error if it can't be
ver_int = version.to_int()


str.to_lower()

將所有字元轉換為小寫

簽章

str to_lower()

範例

target = 'x86_FreeBSD'
lower = target.to_lower() # t now has the value 'x86_freebsd'


str.to_upper()

將所有字元轉換為大寫

簽章

str to_upper()

範例

target = 'x86_FreeBSD'
upper = target.to_upper() # t now has the value 'X86_FREEBSD'


str.underscorify()

建立一個字串,其中每個非字母非數字字元都將被替換為 _

簽章

str underscorify()

範例

name = 'Meson Docs.txt#Reference-manual'
# Replaces all characters other than `a-zA-Z0-9` with `_` (underscore)
# Useful for substituting into #defines, filenames, etc.
underscored = name.underscorify()
# underscored now has the value 'Meson_Docs_txt_Reference_manual'


str.version_compare()

執行語義版本比較。

簽章

# Does semantic version comparison
bool version_compare(
  str compare_string,     # The string to compare to
)

範例

version = '1.2.3'
# Compare version numbers semantically
is_new = version.version_compare('>=2.0')
# is_new now has the boolean value false
# Supports the following operators: '>', '<', '>=', '<=', '!=', '==', '='

Meson 版本比較慣例包括

'3.6'.version_compare('>=3.6.0') == false

最好是明確的,並指定完整的修訂級別來進行比較。

參數

方法 str.version_compare() 接受以下位置參數

名稱 類型 描述 標籤
compare_string 字串

要比較的字串。


搜尋結果為