StringName
A built-in type for unique strings.
StringName.swift:25class StringName
StringName
s are immutable strings designed for general-purpose representation of unique names (also called “string interning”). Two StringName
s with the same value are the same object. Comparing them is extremely fast compared to regular String
s.
You will usually pass a String
to methods expecting a StringName
and it will be automatically converted (often at compile time), but in rare cases you can construct a StringName
ahead of time with the StringName
constructor or, in GDScript, the literal syntax &"example"
. Manually constructing a StringName
allows you to control when the conversion from String
occurs or to use the literal and prevent conversions entirely.
See also NodePath
, which is a similar concept specifically designed to store pre-parsed scene tree paths.
All of String
’s methods are available in this class too. They convert the StringName
into a string, and they also return a string. This is highly inefficient and should only be used if the string is desired.
Citizens in SwiftGodot
Conformances
protocol ContentVariantRepresentable
Some of Godot’s builtin classes use ContentType for storage. This needs to be public because it affects their initialization, but SwiftGodot users should never need to conform their types to
ContentVariantRepresentable
.protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Escapable
protocol ExpressibleByExtendedGraphemeClusterLiteral
A type that can be initialized with a string literal containing a single extended grapheme cluster.
protocol ExpressibleByStringInterpolation
A type that can be initialized by string interpolation with a string literal that includes expressions.
protocol ExpressibleByStringLiteral
A type that can be initialized with a string literal.
protocol ExpressibleByUnicodeScalarLiteral
A type that can be initialized with a string literal containing a single Unicode scalar value.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol LosslessStringConvertible
A type that can be represented as a string in a lossless, unambiguous way.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Typealiases
Type members
init(
) Constructs an empty
StringName
.init(String
) LosslessStringConvertible conformance
init(String.SubSequence
) Creates a StringName from a Swift String.Substring
init(alreadyOwnedContent: ContentType
) init(content: ContentType
) init(from: StringName
) Constructs a
StringName
as a copy of the givenStringName
.init(from: String
) Creates a new
StringName
from the givenString
. In GDScript,StringName("example")
is equivalent to&"example"
.init(fromPtr: UnsafeRawPointer?
) init(stringLiteral: String
) ExpressibleByStringLiteral conformace
static var godotType: Variant.GType
static let zero: ContentType
static func != (lhs: StringName, rhs: StringName
) -> Bool Returns
true
if theStringName
andright
do not refer to the same name. Comparisons betweenStringName
s are much faster than regularString
comparisons.static func != (lhs: StringName, rhs: String
) -> Bool Returns
true
if thisStringName
is not equivalent to the givenString
.static func % (lhs: StringName, rhs: PackedStringArray
) -> String static func % (lhs: StringName, rhs: Projection
) -> String static func % (lhs: StringName, rhs: Quaternion
) -> String static func % (lhs: StringName, rhs: GDictionary
) -> String static func % (lhs: StringName, rhs: Transform2D
) -> String static func % (lhs: StringName, rhs: Transform3D
) -> String static func % (lhs: StringName, rhs: PackedByteArray
) -> String static func % (lhs: StringName, rhs: PackedColorArray
) -> String static func % (lhs: StringName, rhs: PackedInt32Array
) -> String static func % (lhs: StringName, rhs: PackedInt64Array
) -> String static func % (lhs: StringName, rhs: PackedFloat32Array
) -> String static func % (lhs: StringName, rhs: PackedFloat64Array
) -> String static func % (lhs: StringName, rhs: PackedVector2Array
) -> String static func % (lhs: StringName, rhs: PackedVector3Array
) -> String static func % (lhs: StringName, rhs: PackedVector4Array
) -> String static func % (lhs: StringName, rhs: AABB
) -> String static func % (lhs: StringName, rhs: Basis
) -> String static func % (lhs: StringName, rhs: Color
) -> String static func % (lhs: StringName, rhs: Plane
) -> String static func % (lhs: StringName, rhs: Rect2
) -> String static func % (lhs: StringName, rhs: GArray
) -> String static func % (lhs: StringName, rhs: Object
) -> String static func % (lhs: StringName, rhs: Rect2i
) -> String static func % (lhs: StringName, rhs: Signal
) -> String static func % (lhs: StringName, rhs: Vector2
) -> String static func % (lhs: StringName, rhs: Vector3
) -> String static func % (lhs: StringName, rhs: Vector4
) -> String static func % (lhs: StringName, rhs: Callable
) -> String static func % (lhs: StringName, rhs: NodePath
) -> String static func % (lhs: StringName, rhs: Vector2i
) -> String static func % (lhs: StringName, rhs: Vector3i
) -> String static func % (lhs: StringName, rhs: Vector4i
) -> String static func % (lhs: StringName, rhs: StringName
) -> String static func % (lhs: StringName, rhs: String
) -> String static func % (lhs: StringName, rhs: Bool
) -> String static func % (lhs: StringName, rhs: Double
) -> String static func % (lhs: StringName, rhs: Int64
) -> String static func + (lhs: StringName, rhs: StringName
) -> String Appends
right
at the end of thisStringName
, returning aString
. This is also known as a string concatenation.static func + (lhs: StringName, rhs: String
) -> String Appends
right
at the end of thisStringName
, returning aString
. This is also known as a string concatenation.static func < (lhs: StringName, rhs: StringName
) -> Bool Returns
true
if the leftStringName
’s pointer comes beforeright
. Note that this will not match their Unicode order.static func <= (lhs: StringName, rhs: StringName
) -> Bool Returns
true
if the leftStringName
’s pointer comes beforeright
or if they are the same. Note that this will not match their Unicode order.static func == (lhs: StringName, rhs: StringName
) -> Bool Compares two StringNames for equality.
static func == (lhs: StringName, rhs: String
) -> Bool Returns
true
if thisStringName
is equivalent to the givenString
.static func > (lhs: StringName, rhs: StringName
) -> Bool Returns
true
if the leftStringName
’s pointer comes afterright
. Note that this will not match their Unicode order.static func >= (lhs: StringName, rhs: StringName
) -> Bool Returns
true
if the leftStringName
’s pointer comes afterright
or if they are the same. Note that this will not match their Unicode order.
Instance members
var content: ContentType
var description: String
Returns a Swift string from the StringName
func beginsWith(text: String
) -> Bool Returns
true
if the string begins with the giventext
. See alsoendsWith(text:)
.func bigrams(
) -> PackedStringArray Returns an array containing the bigrams (pairs of consecutive characters) of this string.
func binToInt(
) -> Int64 Converts the string representing a binary number into an integer. The string may optionally be prefixed with
"0b"
, and an additional-
prefix for negative numbers.func cEscape(
) -> String Returns a copy of the string with special characters escaped using the C language standard.
func cUnescape(
) -> String Returns a copy of the string with escaped characters replaced by their meanings. Supported escape sequences are
\'
,\"
,\\
,\a
,\b
,\f
,\n
,\r
,\t
,\v
.func capitalize(
) -> String Changes the appearance of the string: replaces underscores (
_
) with spaces, adds spaces before uppercase letters in the middle of a word, converts all letters to lowercase, then converts the first one and each one following a space to uppercase.func casecmpTo(String
) -> Int64 Performs a case-sensitive comparison to another string. Returns
-1
if less than,1
if greater than, or0
if equal. “Less than” and “greater than” are determined by the Unicode code points of each string, which roughly matches the alphabetical order.func contains(what: String
) -> Bool Returns
true
if the string containswhat
. In GDScript, this corresponds to thein
operator.func containsn(what: String
) -> Bool Returns
true
if the string containswhat
, ignoring case.func count(what: String, from: Int64, to: Int64
) -> Int64 Returns the number of occurrences of the substring
what
betweenfrom
andto
positions. Ifto
is 0, the search continues until the end of the string.func countn(what: String, from: Int64, to: Int64
) -> Int64 Returns the number of occurrences of the substring
what
betweenfrom
andto
positions, ignoring case. Ifto
is 0, the search continues until the end of the string.func dedent(
) -> String Returns a copy of the string with indentation (leading tabs and spaces) removed. See also
indent(prefix:)
to add indentation.func endsWith(text: String
) -> Bool Returns
true
if the string ends with the giventext
. See alsobeginsWith(text:)
.func erase(position: Int64, chars: Int64
) -> String Returns a string with
chars
characters erased starting fromposition
. Ifchars
goes beyond the string’s length given the specifiedposition
, fewer characters will be erased from the returned string. Returns an empty string if eitherposition
orchars
is negative. Returns the original string unmodified ifchars
is0
.func filecasecmpTo(String
) -> Int64 Like
naturalcasecmpTo(to:)
but prioritizes strings that begin with periods (.
) and underscores (_
) before any other character. Useful when sorting folders or file names.func filenocasecmpTo(String
) -> Int64 Like
naturalnocasecmpTo(to:)
but prioritizes strings that begin with periods (.
) and underscores (_
) before any other character. Useful when sorting folders or file names.func find(what: String, from: Int64
) -> Int64 Returns the index of the first occurrence of
what
in this string, or-1
if there are none. The search’s start can be specified withfrom
, continuing to the end of the string.func findn(what: String, from: Int64
) -> Int64 Returns the index of the first case-insensitive occurrence of
what
in this string, or-1
if there are none. The starting search index can be specified withfrom
, continuing to the end of the string.func format(values: Variant, placeholder: String
) -> String Formats the string by replacing all occurrences of
placeholder
with the elements ofvalues
.func getBaseDir(
) -> String If the string is a valid file path, returns the base directory name.
func getBasename(
) -> String If the string is a valid file path, returns the full file path, without the extension.
func getExtension(
) -> String If the string is a valid file name or path, returns the file extension without the leading period (
.
). Otherwise, returns an empty string.func getFile(
) -> String If the string is a valid file path, returns the file name, including the extension.
func getSlice(delimiter: String, slice: Int64
) -> String Splits the string using a
delimiter
and returns the substring at indexslice
. Returns an empty string if theslice
does not exist.func getSliceCount(delimiter: String
) -> Int64 Returns the total number of slices when the string is split with the given
delimiter
(seesplit(delimiter:allowEmpty:maxsplit:)
).func getSlicec(delimiter: Int64, slice: Int64
) -> String Splits the string using a Unicode character with code
delimiter
and returns the substring at indexslice
. Returns an empty string if theslice
does not exist.func hash(
) -> Int64 Returns the 32-bit hash value representing the string’s contents.
func hash(into: inout Hasher
) func hexDecode(
) -> PackedByteArray Decodes a hexadecimal string as a
PackedByteArray
.func hexToInt(
) -> Int64 Converts the string representing a hexadecimal number into an integer. The string may be optionally prefixed with
"0x"
, and an additional-
prefix for negative numbers.func indent(prefix: String
) -> String Indents every line of the string with the given
prefix
. Empty lines are not indented. See alsodedent
to remove indentation.func insert(position: Int64, what: String
) -> String Inserts
what
at the givenposition
in the string.func isAbsolutePath(
) -> Bool Returns
true
if the string is a path to a file or directory, and its starting point is explicitly defined. This method is the opposite ofisRelativePath
.func isEmpty(
) -> Bool Returns
true
if the string’s length is0
(""
). See alsolength
.func isRelativePath(
) -> Bool Returns
true
if the string is a path, and its starting point is dependent on context. The path could begin from the current directory, or the currentNode
(if the string is derived from aNodePath
), and may sometimes be prefixed with"./"
. This method is the opposite ofisAbsolutePath
.func isSubsequenceOf(text: String
) -> Bool Returns
true
if all characters of this string can be found intext
in their original order.func isSubsequenceOfn(text: String
) -> Bool Returns
true
if all characters of this string can be found intext
in their original order, ignoring case.func isValidFilename(
) -> Bool Returns
true
if this string does not contain characters that are not allowed in file names (:
/
\
?
*
"
|
%
<
>
).func isValidFloat(
) -> Bool Returns
true
if this string represents a valid floating-point number. A valid float may contain only digits, one decimal point (.
), and the exponent letter (e
). It may also be prefixed with a positive (+
) or negative (-
) sign. Any valid integer is also a valid float (seeisValidInt
). See alsotoFloat
.func isValidHexNumber(withPrefix: Bool
) -> Bool Returns
true
if this string is a valid hexadecimal number. A valid hexadecimal number only contains digits or lettersA
toF
(either uppercase or lowercase), and may be prefixed with a positive (+
) or negative (-
) sign.func isValidHtmlColor(
) -> Bool Returns
true
if this string is a valid color in hexadecimal HTML notation. The string must be a hexadecimal value (seeisValidHexNumber(withPrefix:)
) of either 3, 4, 6 or 8 digits, and may be prefixed by a hash sign (#
). Other HTML notations for colors, such as names orhsl()
, are not considered valid. See alsohtml(rgba:)
.func isValidIdentifier(
) -> Bool Returns
true
if this string is a valid identifier. A valid identifier may contain only letters, digits and underscores (_
), and the first character may not be a digit.func isValidInt(
) -> Bool Returns
true
if this string represents a valid integer. A valid integer only contains digits, and may be prefixed with a positive (+
) or negative (-
) sign. See alsotoInt
.func isValidIpAddress(
) -> Bool Returns
true
if this string represents a well-formatted IPv4 or IPv6 address. This method considers reserved IP addresses such as"0.0.0.0"
and"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
as valid.func join(parts: PackedStringArray
) -> String Returns the concatenation of
parts
’ elements, with each element separated by the string calling this method. This method is the opposite ofsplit(delimiter:allowEmpty:maxsplit:)
.func jsonEscape(
) -> String Returns a copy of the string with special characters escaped using the JSON standard. Because it closely matches the C standard, it is possible to use
cUnescape
to unescape the string, if necessary.func left(length: Int64
) -> String Returns the first
length
characters from the beginning of the string. Iflength
is negative, strips the lastlength
characters from the string’s end.func length(
) -> Int64 Returns the number of characters in the string. Empty strings (
""
) always return0
. See alsoisEmpty
.func lpad(minLength: Int64, character: String
) -> String Formats the string to be at least
minLength
long by addingcharacter
s to the left of the string, if necessary. See alsorpad(minLength:character:)
.func lstrip(chars: String
) -> String Removes a set of characters defined in
chars
from the string’s beginning. See alsorstrip(chars:)
.func match(expr: String
) -> Bool Does a simple expression match (also called “glob” or “globbing”), where
*
matches zero or more arbitrary characters and?
matches any single character except a period (.
). An empty string or empty expression always evaluates tofalse
.func matchn(expr: String
) -> Bool Does a simple case-insensitive expression match, where
*
matches zero or more arbitrary characters and?
matches any single character except a period (.
). An empty string or empty expression always evaluates tofalse
.func md5Buffer(
) -> PackedByteArray Returns the MD5 hash of the string as a
PackedByteArray
.func md5Text(
) -> String func naturalcasecmpTo(String
) -> Int64 Performs a case-sensitive, natural order comparison to another string. Returns
-1
if less than,1
if greater than, or0
if equal. “Less than” or “greater than” are determined by the Unicode code points of each string, which roughly matches the alphabetical order.func naturalnocasecmpTo(String
) -> Int64 Performs a case-insensitive, natural order comparison to another string. Returns
-1
if less than,1
if greater than, or0
if equal. “Less than” or “greater than” are determined by the Unicode code points of each string, which roughly matches the alphabetical order. Internally, lowercase characters are converted to uppercase for the comparison.func nocasecmpTo(String
) -> Int64 Performs a case-insensitive comparison to another string. Returns
-1
if less than,1
if greater than, or0
if equal. “Less than” or “greater than” are determined by the Unicode code points of each string, which roughly matches the alphabetical order. Internally, lowercase characters are converted to uppercase for the comparison.func padDecimals(digits: Int64
) -> String Formats the string representing a number to have an exact number of
digits
after the decimal point.func padZeros(digits: Int64
) -> String Formats the string representing a number to have an exact number of
digits
before the decimal point.func pathJoin(file: String
) -> String Concatenates
file
at the end of the string as a subpath, adding/
if necessary.func replace(what: String, forwhat: String
) -> String Replaces all occurrences of
what
inside the string with the givenforwhat
.func replacen(what: String, forwhat: String
) -> String Replaces all case-insensitive occurrences of
what
inside the string with the givenforwhat
.func reverse(
) -> String Returns the copy of this string in reverse order. This operation works on unicode codepoints, rather than sequences of codepoints, and may break things like compound letters or emojis.
func rfind(what: String, from: Int64
) -> Int64 Returns the index of the last occurrence of
what
in this string, or-1
if there are none. The search’s start can be specified withfrom
, continuing to the beginning of the string. This method is the reverse offind(what:from:)
.func rfindn(what: String, from: Int64
) -> Int64 Returns the index of the last case-insensitive occurrence of
what
in this string, or-1
if there are none. The starting search index can be specified withfrom
, continuing to the beginning of the string. This method is the reverse offindn(what:from:)
.func right(length: Int64
) -> String Returns the last
length
characters from the end of the string. Iflength
is negative, strips the firstlength
characters from the string’s beginning.func rpad(minLength: Int64, character: String
) -> String Formats the string to be at least
minLength
long, by addingcharacter
s to the right of the string, if necessary. See alsolpad(minLength:character:)
.func rsplit(delimiter: String, allowEmpty: Bool, maxsplit: Int64
) -> PackedStringArray Splits the string using a
delimiter
and returns an array of the substrings, starting from the end of the string. The splits in the returned array appear in the same order as the original string. Ifdelimiter
is an empty string, each substring will be a single character.func rstrip(chars: String
) -> String Removes a set of characters defined in
chars
from the string’s end. See alsolstrip(chars:)
.func sha1Buffer(
) -> PackedByteArray Returns the SHA-1 hash of the string as a
PackedByteArray
.func sha1Text(
) -> String func sha256Buffer(
) -> PackedByteArray Returns the SHA-256 hash of the string as a
PackedByteArray
.func sha256Text(
) -> String func similarity(text: String
) -> Double Returns the similarity index (Sorensen-Dice coefficient) of this string compared to another. A result of
1.0
means totally similar, while0.0
means totally dissimilar.func simplifyPath(
) -> String If the string is a valid file path, converts the string into a canonical path. This is the shortest possible path, without
"./"
, and all the unnecessary".."
and"/"
.func split(delimiter: String, allowEmpty: Bool, maxsplit: Int64
) -> PackedStringArray Splits the string using a
delimiter
and returns an array of the substrings. Ifdelimiter
is an empty string, each substring will be a single character. This method is the opposite ofjoin(parts:)
.func splitFloats(delimiter: String, allowEmpty: Bool
) -> PackedFloat64Array Splits the string into floats by using a
delimiter
and returns aPackedFloat64Array
.func stripEdges(left: Bool, right: Bool
) -> String Strips all non-printable characters from the beginning and the end of the string. These include spaces, tabulations (
\t
), and newlines (\n
\r
).func stripEscapes(
) -> String Strips all escape characters from the string. These include all non-printable control characters of the first page of the ASCII table (values from 0 to 31), such as tabulation (
\t
) and newline (\n
,\r
) characters, but not spaces.func substr(from: Int64, len: Int64
) -> String Returns part of the string from the position
from
with lengthlen
. Iflen
is-1
(as by default), returns the rest of the string starting from the given position.func toAsciiBuffer(
) -> PackedByteArray Converts the string to an ASCII/Latin-1 encoded
PackedByteArray
. This method is slightly faster thantoUtf8Buffer
, but replaces all unsupported characters with spaces.func toCamelCase(
) -> String Returns the string converted to
camelCase
.func toFloat(
) -> Double Converts the string representing a decimal number into a float. This method stops on the first non-number character, except the first decimal point (
.
) and the exponent letter (e
). See alsoisValidFloat
.func toInt(
) -> Int64 Converts the string representing an integer number into an integer. This method removes any non-number character and stops at the first decimal point (
.
). See alsoisValidInt
.func toLower(
) -> String Returns the string converted to
lowercase
.func toPascalCase(
) -> String Returns the string converted to
PascalCase
.func toSnakeCase(
) -> String Returns the string converted to
snake_case
.func toUpper(
) -> String Returns the string converted to
UPPERCASE
.func toUtf16Buffer(
) -> PackedByteArray Converts the string to a UTF-16 encoded
PackedByteArray
.func toUtf32Buffer(
) -> PackedByteArray Converts the string to a UTF-32 encoded
PackedByteArray
.func toUtf8Buffer(
) -> PackedByteArray Converts the string to a UTF-8 encoded
PackedByteArray
. This method is slightly slower thantoAsciiBuffer
, but supports all UTF-8 characters. For most cases, prefer using this method.func toWcharBuffer(
) -> PackedByteArray Converts the string to a wide character (
wchar_t
, UTF-16 on Windows, UTF-32 on other platforms) encodedPackedByteArray
.func trimPrefix(String
) -> String Removes the given
prefix
from the start of the string, or returns the string unchanged.func trimSuffix(String
) -> String Removes the given
suffix
from the end of the string, or returns the string unchanged.func unicodeAt(Int64
) -> Int64 Returns the character code at position
at
.func uriDecode(
) -> String Decodes the string from its URL-encoded format. This method is meant to properly decode the parameters in a URL when receiving an HTTP request.
func uriEncode(
) -> String Encodes the string to URL-friendly format. This method is meant to properly encode the parameters in a URL when sending an HTTP request.
func validateFilename(
) -> String Returns a copy of the string with all characters that are not allowed in
isValidFilename
replaced with underscores.func validateNodeName(
) -> String Returns a copy of the string with all characters that are not allowed in
name
(.
:
@
/
"
%
) replaced with underscores.func xmlEscape(escapeQuotes: Bool
) -> String Returns a copy of the string with special characters escaped using the XML standard. If
escapeQuotes
istrue
, the single quote ('
) and double quote ("
) characters are also escaped.func xmlUnescape(
) -> String Returns a copy of the string with escaped characters replaced by their meanings according to the XML standard.
Type features
init?(Variant
) init(extendedGraphemeClusterLiteral: Self.StringLiteralType
) init(stringInterpolation: DefaultStringInterpolation
) Creates a new instance from an interpolated string literal.
init(unicodeScalarLiteral: Self.ExtendedGraphemeClusterLiteralType
) static func makeOrUnwrap(Variant
) -> Self? Unwraps an object from a variant.
static func makeOrUnwrap(Variant
) -> Self? Unwraps an object from a variant. This is useful when you want one method to call that will return the unwrapped Variant, regardless of whether it is a SwiftGodot.Object or not.
static func != (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether two values are not equal.