Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

String

字符串内置类型。

描述

这是内置的字符串 Variant 类型(GDScript 使用的就是这个类型)。字符串中可以包含任意数量的 Unicode 字符,暴露的方法可用于字符串的操作和生成。字符串有引用计数,使用写时复制技术(每次对字符串的修改都会返回新的 String),所以传递字符串的资源损耗很小。

部分字符串方法有对应的变体。后缀 n 的变体(countnfindnreplacen 等)大小写不敏感(不区分大写字符和小写字符)。前缀 r 的方法变体(rfindrsplit 等)是逆序的,会从字符串末尾开始,而不是从开头开始。

注意:在布尔语境下,空字符串("")的求值结果为 false。否则字符串的求值结果始终为 true。无法使用 not 运算符。检查空字符串请使用 is_empty

备注

通过 C# 使用这个 API 时有显著的不同。详见 C# API 与 GDScript 的差异

教程

构造函数

String

String()

String

String(from: String)

String

String(from: NodePath)

String

String(from: StringName)

方法

bool

begins_with(text: String) const

PackedStringArray

bigrams() const

int

bin_to_int() const

String

c_escape() const

String

c_unescape() const

String

capitalize() const

int

casecmp_to(to: String) const

String

chr(char: int) static

bool

contains(what: String) const

bool

containsn(what: String) const

int

count(what: String, from: int = 0, to: int = 0) const

int

countn(what: String, from: int = 0, to: int = 0) const

String

dedent() const

bool

ends_with(text: String) const

String

erase(position: int, chars: int = 1) const

int

filecasecmp_to(to: String) const

int

filenocasecmp_to(to: String) const

int

find(what: String, from: int = 0) const

int

findn(what: String, from: int = 0) const

String

format(values: Variant, placeholder: String = "{_}") const

String

get_base_dir() const

String

get_basename() const

String

get_extension() const

String

get_file() const

String

get_slice(delimiter: String, slice: int) const

int

get_slice_count(delimiter: String) const

String

get_slicec(delimiter: int, slice: int) const

int

hash() const

PackedByteArray

hex_decode() const

int

hex_to_int() const

String

humanize_size(size: int) static

String

indent(prefix: String) const

String

insert(position: int, what: String) const

bool

is_absolute_path() const

bool

is_empty() const

bool

is_relative_path() const

bool

is_subsequence_of(text: String) const

bool

is_subsequence_ofn(text: String) const

bool

is_valid_filename() const

bool

is_valid_float() const

bool

is_valid_hex_number(with_prefix: bool = false) const

bool

is_valid_html_color() const

bool

is_valid_identifier() const

bool

is_valid_int() const

bool

is_valid_ip_address() const

String

join(parts: PackedStringArray) const

String

json_escape() const

String

left(length: int) const

int

length() const

String

lpad(min_length: int, character: String = " ") const

String

lstrip(chars: String) const

bool

match(expr: String) const

bool

matchn(expr: String) const

PackedByteArray

md5_buffer() const

String

md5_text() const

int

naturalcasecmp_to(to: String) const

int

naturalnocasecmp_to(to: String) const

int

nocasecmp_to(to: String) const

String

num(number: float, decimals: int = -1) static

String

num_int64(number: int, base: int = 10, capitalize_hex: bool = false) static

String

num_scientific(number: float) static

String

num_uint64(number: int, base: int = 10, capitalize_hex: bool = false) static

String

pad_decimals(digits: int) const

String

pad_zeros(digits: int) const

String

path_join(file: String) const

String

repeat(count: int) const

String

replace(what: String, forwhat: String) const

String

replacen(what: String, forwhat: String) const

String

reverse() const

int

rfind(what: String, from: int = -1) const

int

rfindn(what: String, from: int = -1) const

String

right(length: int) const

String

rpad(min_length: int, character: String = " ") const

PackedStringArray

rsplit(delimiter: String = "", allow_empty: bool = true, maxsplit: int = 0) const

String

rstrip(chars: String) const

PackedByteArray

sha1_buffer() const

String

sha1_text() const

PackedByteArray

sha256_buffer() const

String

sha256_text() const

float

similarity(text: String) const

String

simplify_path() const

PackedStringArray

split(delimiter: String = "", allow_empty: bool = true, maxsplit: int = 0) const

PackedFloat64Array

split_floats(delimiter: String, allow_empty: bool = true) const

String

strip_edges(left: bool = true, right: bool = true) const

String

strip_escapes() const

String

substr(from: int, len: int = -1) const

PackedByteArray

to_ascii_buffer() const

String

to_camel_case() const

float

to_float() const

int

to_int() const

String

to_lower() const

String

to_pascal_case() const

String

to_snake_case() const

String

to_upper() const

PackedByteArray

to_utf8_buffer() const

PackedByteArray

to_utf16_buffer() const

PackedByteArray

to_utf32_buffer() const

PackedByteArray

to_wchar_buffer() const

String

trim_prefix(prefix: String) const

String

trim_suffix(suffix: String) const

int

unicode_at(at: int) const

String

uri_decode() const

String

uri_encode() const

String

validate_filename() const

String

validate_node_name() const

String

xml_escape(escape_quotes: bool = false) const

String

xml_unescape() const

操作符

bool

operator !=(right: String)

bool

operator !=(right: StringName)

String

operator %(right: Variant)

String

operator +(right: String)

String

operator +(right: StringName)

bool

operator <(right: String)

bool

operator <=(right: String)

bool

operator ==(right: String)

bool

operator ==(right: StringName)

bool

operator >(right: String)

bool

operator >=(right: String)

String

operator [](index: int)


构造函数说明

String String() 🔗

构造空的 String"")。


String String(from: String)

构造给定 String 的副本。


String String(from: NodePath)

从给定的 NodePath 构造新的 String


String String(from: StringName)

从给定的 StringName 构造新 String


方法说明

bool begins_with(text: String) const 🔗

如果该字符串以给定的 text 开始,则返回 true。另见 ends_with


PackedStringArray bigrams() const 🔗

返回包含该字符串的双字母组(连续字母的组合)的数组。

print("Get up!".bigrams())