文字列 (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()は、以下の位置引数を受け付けます。

名前 説明 タグ
fragment str

確認する文字列フラグメント


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()は、以下の位置引数を受け付けます。

名前 説明 タグ
fragment str

確認する文字列フラグメント


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 str

フォーマットする文字列。

フォーマットは、@number@型のプレースホルダーを対応する可変引数で置き換えることによって機能します。

さらに、このメソッドは、int | bool | str型の可変個の引数(value...)を0個から無限個受け付けます。

フォーマット文字列の@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'

引数

このメソッドは、str型の可変個の引数(strings...)を0個から無限個受け付けます。

現在の文字列と結合する文字列。

Meson 0.60.0 より前では、この関数は[[list[str]]]型の位置引数を1つだけ受け付けていました。

(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 str

検索する部分文字列

new str

置換文字列


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

文字列を分割する文字/部分文字列を指定します。

[オプション]


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()は、以下の位置引数を受け付けます。

名前 説明 タグ
fragment str

確認する文字列フラグメント


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 str

空白の代わりに、この文字列内のすべての文字を削除します。

(0.43.0以降)

[オプション]


str.substring()

startからendまで指定された部分文字列を返します。startendの両方の引数はオプションです。そのため、例えば'foobar'.substring()'foobar'を返します。

このメソッドは、負のstartが文字列の末尾からの相対位置len(string) - startである場合、および負のendでも負の値を受け付けます。

startまたはendが範囲外の場合、最も近い文字の位置が使用されます。startendより大きい場合、結果は空の部分文字列になります。

シグネチャ

(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に変換し、変換できない場合はエラーをスローします。

シグネチャ

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 str

比較する文字列。


検索の結果は、