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.

StringName

唯一字符串内置类型。

描述

StringName 是不可变的字符串,用于唯一名称的通用表示(也叫“字符串内嵌”)。值相同的两个 StringName 是同一个对象。进行比较时比普通 String 要快很多。

需要 StringName 的方法通常可以传 String,会自动进行转换(经常发生在编译时),不过有时候你可能会想要提前使用 StringName 构造函数来构造 StringName,在 GDScript 中也可以用字面量语法 &"example"。你可以通过手动构造 StringName 来控制 String 的转换时机,使用字面量则可以完全消除掉转换过程。

另见 NodePath,这是与此类似的概念,针对存储预解析的场景树路径设计。

String 的所有方法都在这个类中可用。它们会将 StringName 转换为字符串,返回的也是字符串。这样做效率非常低,应该只在需要字符串时使用。

注意:在 C# 中,需要显式转换为 System.String 才能使用本页列出的方法。使用 ToString() 方法将 StringName 转换为字符串,然后使用 System.StringStringExtensions 中的等效方法。

注意:转换为布尔值时,空的 StringNameStringName(""))为 false,其他 StringName 均为 true。不能使用 not 运算符。请改用 is_empty 来检查空的 StringName

备注

通过 C# 使用该 API 时会有显著不同,详见 C# API 与 GDScript 的差异

构造函数

StringName

StringName()

StringName

StringName(from: StringName)

StringName

StringName(from: String)

方法

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

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

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

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: StringName)

bool

operator <=(right: StringName)

bool

operator ==(right: String)

bool

operator ==(right: StringName)

bool

operator >(right: StringName)

bool

operator >=(right: StringName)


构造函数说明

StringName StringName() 🔗

构造空的 StringName


StringName StringName(from: StringName)

构造给定 StringName 的副本。


StringName StringName(from: String)

从给定的 String 创建 StringName。在 GDScript 中,StringName("example")&"example" 等价。


方法说明

bool begins_with(text: String) const 🔗

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


PackedStringArray bigrams() const 🔗

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