Table of Contents

Class StringExtensions

Namespace
Godot
Assembly
GodotSharp.dll

Extension methods to manipulate strings.

public static class StringExtensions
Inheritance
StringExtensions
Inherited Members

Methods

Bigrams(string)

Returns the bigrams (pairs of consecutive letters) of this string.

public static string[] Bigrams(this string instance)

Parameters

instance string

The string that will be used.

Returns

string[]

The bigrams of this string.

BinToInt(string)

Converts a string containing a binary number into an integer. Binary strings can either be prefixed with 0b or not, and they can also start with a - before the optional prefix.

public static int BinToInt(this string instance)

Parameters

instance string

The string to convert.

Returns

int

The converted string.

CEscape(string)

Returns a copy of the string with special characters escaped using the C language standard.

public static string CEscape(this string instance)

Parameters

instance string

The string to escape.

Returns

string

The escaped string.

CUnescape(string)

Returns a copy of the string with escaped characters replaced by their meanings according to the C language standard.

public static string CUnescape(this string instance)

Parameters

instance string

The string to unescape.

Returns

string

The unescaped string.

Capitalize(string)

Changes the case of some letters. Replace underscores with spaces, convert all letters to lowercase then capitalize first and every letter following the space character. For capitalize camelCase mixed_with_underscores it will return Capitalize Camelcase Mixed With Underscores.

public static string Capitalize(this string instance)

Parameters

instance string

The string to capitalize.

Returns

string

The capitalized string.

CasecmpTo(string, string)

Performs a case-sensitive comparison to another string, return -1 if less, 0 if equal and +1 if greater.

public static int CasecmpTo(this string instance, string to)

Parameters

instance string

The string to compare.

to string

The other string to compare.

Returns

int

-1 if less, 0 if equal and +1 if greater.

See Also

CompareTo(string, string, bool)

Performs a comparison to another string, return -1 if less, 0 if equal and +1 if greater.

public static int CompareTo(this string instance, string to, bool caseSensitive = true)

Parameters

instance string

The string to compare.

to string

The other string to compare.

caseSensitive bool

If true, the comparison will be case sensitive.

Returns

int

-1 if less, 0 if equal and +1 if greater.

Count(string, string, int, int, bool)

Returns the number of occurrences of substring what in the string.

public static int Count(this string instance, string what, int from = 0, int to = 0, bool caseSensitive = true)

Parameters

instance string

The string where the substring will be searched.

what string

The substring that will be counted.

from int

Index to start searching from.

to int

Index to stop searching at.

caseSensitive bool

If the search is case sensitive.

Returns

int

Number of occurrences of the substring in the string.

CountN(string, string, int, int)

Returns the number of occurrences of substring what (ignoring case) between from and to positions. If from and to equals 0 the whole string will be used. If only to equals 0 the remained substring will be used.

public static int CountN(this string instance, string what, int from = 0, int to = 0)

Parameters

instance string

The string where the substring will be searched.

what string

The substring that will be counted.

from int

Index to start searching from.

to int

Index to stop searching at.

Returns

int

Number of occurrences of the substring in the string.

Dedent(string)

Returns a copy of the string with indentation (leading tabs and spaces) removed. See also Indent(string, string) to add indentation.

public static string Dedent(this string instance)

Parameters

instance string

The string to remove the indentation from.

Returns

string

The string with the indentation removed.

Find(string, char, int, bool)

Find the first occurrence of a char. Optionally, the search starting position can be passed.

public static int Find(this string instance, char what, int from = 0, bool caseSensitive = true)

Parameters

instance string

The string that will be searched.

what char

The substring to find.

from int

The search starting position.

caseSensitive bool

If true, the search is case sensitive.

Returns

int

The first instance of the char, or -1 if not found.

See Also

Find(string, string, int, bool)

Returns the index of the first occurrence of the specified string in this instance, or -1. Optionally, the starting search index can be specified, continuing to the end of the string. Note: If you just want to know whether a string contains a substring, use the Contains(string) method.

public static int Find(this string instance, string what, int from = 0, bool caseSensitive = true)

Parameters

instance string

The string that will be searched.

what string

The substring to find.

from int

The search starting position.

caseSensitive bool

If true, the search is case sensitive.

Returns

int

The starting position of the substring, or -1 if not found.

See Also

FindN(string, string, int)

Returns the index of the first case-insensitive occurrence of the specified string in this instance, or -1. Optionally, the starting search index can be specified, continuing to the end of the string.

public static int FindN(this string instance, string what, int from = 0)

Parameters

instance string

The string that will be searched.

what string

The substring to find.

from int

The search starting position.

Returns

int

The starting position of the substring, or -1 if not found.

See Also

GetBaseDir(string)

If the string is a path to a file, return the base directory.

public static string GetBaseDir(this string instance)

Parameters

instance string

The path to a file.

Returns

string

The base directory.

See Also

GetBaseName(string)

If the string is a path to a file, return the path to the file without the extension.

public static string GetBaseName(this string instance)

Parameters

instance string

The path to a file.

Returns

string

The path to the file without the extension.

See Also

GetExtension(string)

Returns the extension without the leading period character (.) if the string is a valid file name or path. If the string does not contain an extension, returns an empty string instead.

public static string GetExtension(this string instance)

Parameters

instance string

The path to a file.

Returns

string

The extension of the file or an empty string.

Examples

GD.Print("/path/to/file.txt".GetExtension())  // "txt"
GD.Print("file.txt".GetExtension())  // "txt"
GD.Print("file.sample.txt".GetExtension())  // "txt"
GD.Print(".txt".GetExtension())  // "txt"
GD.Print("file.txt.".GetExtension())  // "" (empty string)
GD.Print("file.txt..".GetExtension())  // "" (empty string)
GD.Print("txt".GetExtension())  // "" (empty string)
GD.Print("".GetExtension())  // "" (empty string)
See Also

GetFile(string)

If the string is a path to a file, return the file and ignore the base directory.

public static string GetFile(this string instance)

Parameters

instance string

The path to a file.

Returns

string

The file name.

See Also

GetStringFromAscii(byte[])

Converts ASCII encoded array to string. Fast alternative to GetStringFromUtf8(byte[]) if the content is ASCII-only. Unlike the UTF-8 function this function maps every byte to a character in the array. Multibyte sequences will not be interpreted correctly. For parsing user input always use GetStringFromUtf8(byte[]).

public static string GetStringFromAscii(this byte[] bytes)

Parameters

bytes byte[]

A byte array of ASCII characters (on the range of 0-127).

Returns

string

A string created from the bytes.

GetStringFromUtf16(byte[])

Converts UTF-16 encoded array to string using the little endian byte order.

public static string GetStringFromUtf16(this byte[] bytes)

Parameters

bytes byte[]

A byte array of UTF-16 characters.

Returns

string

A string created from the bytes.

GetStringFromUtf32(byte[])

Converts UTF-32 encoded array to string using the little endian byte order.

public static string GetStringFromUtf32(this byte[] bytes)

Parameters

bytes byte[]

A byte array of UTF-32 characters.

Returns

string

A string created from the bytes.

GetStringFromUtf8(byte[])

Converts UTF-8 encoded array to string. Slower than GetStringFromAscii(byte[]) but supports UTF-8 encoded data. Use this function if you are unsure about the source of the data. For user input this function should always be preferred.

public static string GetStringFromUtf8(this byte[] bytes)

Parameters

bytes byte[]

A byte array of UTF-8 characters (a character may take up multiple bytes).

Returns

string

A string created from the bytes.

Hash(string)

Hash the string and return a 32 bits unsigned integer.

public static uint Hash(this string instance)

Parameters

instance string

The string to hash.

Returns

uint

The calculated hash of the string.

HexDecode(string)

Decodes a hexadecimal string.

public static byte[] HexDecode(this string instance)

Parameters

instance string

The hexadecimal string.

Returns

byte[]

The byte array representation of this string.

HexEncode(byte[])

Returns a hexadecimal representation of this byte array as a string.

public static string HexEncode(this byte[] bytes)

Parameters

bytes byte[]

The byte array to encode.

Returns

string

The hexadecimal representation of this byte array.

HexToInt(string)

Converts a string containing a hexadecimal number into an integer. Hexadecimal strings can either be prefixed with 0x or not, and they can also start with a - before the optional prefix.

public static int HexToInt(this string instance)

Parameters

instance string

The string to convert.

Returns

int

The converted string.

Indent(string, string)

Returns a copy of the string with lines indented with prefix. For example, the string can be indented with two tabs using "\t\t", or four spaces using " ". The prefix can be any string so it can also be used to comment out strings with e.g. "// . See also Dedent(string) to remove indentation. Note: Empty lines are kept empty.

public static string Indent(this string instance, string prefix)

Parameters

instance string

The string to add indentation to.

prefix string

The string to use as indentation.

Returns

string

The string with indentation added.

IsAbsolutePath(string)

Returns true if the string is a path to a file or directory and its starting point is explicitly defined. This includes res://, user://, C:</code>, /, etc.

public static bool IsAbsolutePath(this string instance)

Parameters

instance string

The string to check.

Returns

bool

If the string is an absolute path.

See Also

IsRelativePath(string)

Returns true if the string is a path to a file or directory and its starting point is implicitly defined within the context it is being used. The starting point may refer to the current directory (./), or the current Node.

public static bool IsRelativePath(this string instance)

Parameters

instance string

The string to check.

Returns

bool

If the string is a relative path.

See Also

IsSubsequenceOf(string, string, bool)

Check whether this string is a subsequence of the given string.

public static bool IsSubsequenceOf(this string instance, string text, bool caseSensitive = true)

Parameters

instance string

The subsequence to search.

text string

The string that contains the subsequence.

caseSensitive bool

If true, the check is case sensitive.

Returns

bool

If the string is a subsequence of the given string.

See Also

IsSubsequenceOfN(string, string)

Check whether this string is a subsequence of the given string, ignoring case differences.

public static bool IsSubsequenceOfN(this string instance, string text)

Parameters

instance string

The subsequence to search.

text string

The string that contains the subsequence.

Returns

bool

If the string is a subsequence of the given string.

See Also

IsValidFileName(string)

Returns true if this string is free from characters that aren't allowed in file names.

public static bool IsValidFileName(this string instance)

Parameters

instance string

The string to check.

Returns

bool

If the string contains a valid file name.

IsValidFloat(string)

Returns true if this string contains a valid float. This is inclusive of integers, and also supports exponents.

public static bool IsValidFloat(this string instance)

Parameters

instance string

The string to check.

Returns

bool

If the string contains a valid floating point number.

Examples

GD.Print("1.7".IsValidFloat())  // Prints "True"
GD.Print("24".IsValidFloat())  // Prints "True"
GD.Print("7e3".IsValidFloat())  // Prints "True"
GD.Print("Hello".IsValidFloat())  // Prints "False"

IsValidHexNumber(string, bool)

Returns true if this string contains a valid hexadecimal number. If withPrefix is true, then a validity of the hexadecimal number is determined by 0x prefix, for instance: 0xDEADC0DE.

public static bool IsValidHexNumber(this string instance, bool withPrefix = false)

Parameters

instance string

The string to check.

withPrefix bool

If the string must contain the 0x prefix to be valid.

Returns

bool

If the string contains a valid hexadecimal number.

IsValidHtmlColor(string)

Returns true if this string contains a valid color in hexadecimal HTML notation. Other HTML notations such as named colors or hsl() aren't considered valid by this method and will return false.

public static bool IsValidHtmlColor(this string instance)

Parameters

instance string

The string to check.

Returns

bool

If the string contains a valid HTML color.

IsValidIPAddress(string)

Returns true if this string contains only a well-formatted IPv4 or IPv6 address. This method considers reserved IP addresses such as 0.0.0.0 as valid.

public static bool IsValidIPAddress(this string instance)

Parameters

instance string

The string to check.

Returns

bool

If the string contains a valid IP address.

IsValidIdentifier(string)

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.

public static bool IsValidIdentifier(this string instance)

Parameters

instance string

The string to check.

Returns

bool

If the string contains a valid identifier.

Examples

GD.Print("good_ident_1".IsValidIdentifier())  // Prints "True"
GD.Print("1st_bad_ident".IsValidIdentifier())  // Prints "False"
GD.Print("bad_ident_#2".IsValidIdentifier())  // Prints "False"

IsValidInt(string)

Returns true if this string contains a valid int.

public static bool IsValidInt(this string instance)

Parameters

instance string

The string to check.

Returns

bool

If the string contains a valid integer.

Examples

GD.Print("7".IsValidInt())  // Prints "True"
GD.Print("14.6".IsValidInt())  // Prints "False"
GD.Print("L".IsValidInt())  // Prints "False"
GD.Print("+3".IsValidInt())  // Prints "True"
GD.Print("-12".IsValidInt())  // Prints "True"

JSONEscape(string)

Returns a copy of the string with special characters escaped using the JSON standard.

public static string JSONEscape(this string instance)

Parameters

instance string

The string to escape.

Returns

string

The escaped string.

Left(string, int)

Returns an amount of characters from the left of the string.

public static string Left(this string instance, int pos)

Parameters

instance string

The original string.

pos int

The position in the string where the left side ends.

Returns

string

The left side of the string from the given position.

See Also

Match(string, string, bool)

Do a simple case sensitive expression match, using ? and * wildcards (see Godot.StringExtensions.ExprMatch(System.String,System.String,System.Boolean)).

public static bool Match(this string instance, string expr, bool caseSensitive = true)

Parameters

instance string

The string to check.

expr string

Expression to check.

caseSensitive bool

If true, the check will be case sensitive.

Returns

bool

If the expression has any matches.

See Also

MatchN(string, string)

Do a simple case insensitive expression match, using ? and * wildcards (see Godot.StringExtensions.ExprMatch(System.String,System.String,System.Boolean)).

public static bool MatchN(this string instance, string expr)

Parameters

instance string

The string to check.

expr string

Expression to check.

Returns

bool

If the expression has any matches.

See Also

Md5Buffer(string)

Returns the MD5 hash of the string as an array of bytes.

public static byte[] Md5Buffer(this string instance)

Parameters

instance string

The string to hash.

Returns

byte[]

The MD5 hash of the string.

See Also

Md5Text(string)

Returns the MD5 hash of the string as a string.

public static string Md5Text(this string instance)

Parameters

instance string

The string to hash.

Returns

string

The MD5 hash of the string.

See Also

NocasecmpTo(string, string)

Perform a case-insensitive comparison to another string, return -1 if less, 0 if equal and +1 if greater.

public static int NocasecmpTo(this string instance, string to)

Parameters

instance string

The string to compare.

to string

The other string to compare.

Returns

int

-1 if less, 0 if equal and +1 if greater.

See Also

PadDecimals(string, int)

Format a number to have an exact number of digits after the decimal point.

public static string PadDecimals(this string instance, int digits)

Parameters

instance string

The string to pad.

digits int

Amount of digits after the decimal point.

Returns

string

The string padded with zeroes.

See Also

PadZeros(string, int)

Format a number to have an exact number of digits before the decimal point.

public static string PadZeros(this string instance, int digits)

Parameters

instance string

The string to pad.

digits int

Amount of digits before the decimal point.

Returns

string

The string padded with zeroes.

See Also

PathJoin(string, string)

If the string is a path, this concatenates file at the end of the string as a subpath. E.g. "this/is".PathJoin("path") == "this/is/path".

public static string PathJoin(this string instance, string file)

Parameters

instance string

The path that will be concatenated.

file string

File name to concatenate with the path.

Returns

string

The concatenated path with the given file name.

RFind(string, string, int, bool)

Returns the index of the last occurrence of the specified string in this instance, or -1. Optionally, the starting search index can be specified, continuing to the beginning of the string.

public static int RFind(this string instance, string what, int from = -1, bool caseSensitive = true)

Parameters

instance string

The string that will be searched.

what string

The substring to search in the string.

from int

The position at which to start searching.

caseSensitive bool

If true, the search is case sensitive.

Returns

int

The position at which the substring was found, or -1 if not found.

See Also

RFindN(string, string, int)

Returns the index of the last case-insensitive occurrence of the specified string in this instance, or -1. Optionally, the starting search index can be specified, continuing to the beginning of the string.

public static int RFindN(this string instance, string what, int from = -1)

Parameters

instance string

The string that will be searched.

what string

The substring to search in the string.

from int

The position at which to start searching.

Returns

int

The position at which the substring was found, or -1 if not found.

See Also

Replace(string, string, string)

Replace occurrences of a substring for different ones inside the string.

public static string Replace(this string instance, string what, string forwhat)

Parameters

instance string

The string to modify.

what string

The substring to be replaced in the string.

forwhat string

The substring that replaces what.

Returns

string

The string with the substring occurrences replaced.

See Also

ReplaceN(string, string, string)

Replace occurrences of a substring for different ones inside the string, but search case-insensitive.

public static string ReplaceN(this string instance, string what, string forwhat)

Parameters

instance string

The string to modify.

what string

The substring to be replaced in the string.

forwhat string

The substring that replaces what.

Returns

string

The string with the substring occurrences replaced.

See Also

Right(string, int)

Returns the right side of the string from a given position.

public static string Right(this string instance, int pos)

Parameters

instance string

The original string.

pos int

The position in the string from which the right side starts.

Returns

string

The right side of the string from the given position.

See Also

Sha1Buffer(string)

Returns the SHA-1 hash of the string as an array of bytes.

public static byte[] Sha1Buffer(this string instance)

Parameters

instance string

The string to hash.

Returns

byte[]

The SHA-1 hash of the string.

See Also

Sha1Text(string)

Returns the SHA-1 hash of the string as a string.

public static string Sha1Text(this string instance)

Parameters

instance string

The string to hash.

Returns

string

The SHA-1 hash of the string.

See Also

Sha256Buffer(string)

Returns the SHA-256 hash of the string as an array of bytes.

public static byte[] Sha256Buffer(this string instance)

Parameters

instance string

The string to hash.

Returns

byte[]

The SHA-256 hash of the string.

See Also

Sha256Text(string)

Returns the SHA-256 hash of the string as a string.

public static string Sha256Text(this string instance)

Parameters

instance string

The string to hash.

Returns

string

The SHA-256 hash of the string.

See Also

Similarity(string, string)

Returns the similarity index of the text compared to this string. 1 means totally similar and 0 means totally dissimilar.

public static float Similarity(this string instance, string text)

Parameters

instance string

The string to compare.

text string

The other string to compare.

Returns

float

The similarity index.

SimplifyPath(string)

Returns a simplified canonical path.

public static string SimplifyPath(this string instance)

Parameters

instance string

Returns

string

Split(string, string, bool)

Split the string by a divisor string, return an array of the substrings. Example "One,Two,Three" will return ["One","Two","Three"] if split by ",".

public static string[] Split(this string instance, string divisor, bool allowEmpty = true)

Parameters

instance string

The string to split.

divisor string

The divisor string that splits the string.

allowEmpty bool

If true, the array may include empty strings.

Returns

string[]

The array of strings split from the string.

See Also

SplitFloats(string, string, bool)

Split the string in floats by using a divisor string, return an array of the substrings. Example "1,2.5,3" will return [1,2.5,3] if split by ",".

public static float[] SplitFloats(this string instance, string divisor, bool allowEmpty = true)

Parameters

instance string

The string to split.

divisor string

The divisor string that splits the string.

allowEmpty bool

If true, the array may include empty floats.

Returns

float[]

The array of floats split from the string.

See Also

StripEdges(string, bool, bool)

Returns a copy of the string stripped of any non-printable character (including tabulations, spaces and line breaks) at the beginning and the end. The optional arguments are used to toggle stripping on the left and right edges respectively.

public static string StripEdges(this string instance, bool left = true, bool right = true)

Parameters

instance string

The string to strip.

left bool

If the left side should be stripped.

right bool

If the right side should be stripped.

Returns

string

The string stripped of any non-printable characters.

StripEscapes(string)

Returns a copy of the string stripped of any escape character. These include all non-printable control characters of the first page of the ASCII table (< 32), such as tabulation (\t) and newline (\n and \r) characters, but not spaces.

public static string StripEscapes(this string instance)

Parameters

instance string

The string to strip.

Returns

string

The string stripped of any escape characters.

Substr(string, int, int)

Returns part of the string from the position from, with length len.

public static string Substr(this string instance, int from, int len)

Parameters

instance string

The string to slice.

from int

The position in the string that the part starts from.

len int

The length of the returned part.

Returns

string

Part of the string from the position from, with length len.

ToAsciiBuffer(string)

Converts the String (which is a character array) to PackedByteArray (which is an array of bytes). The conversion is faster compared to ToUtf8Buffer(string), as this method assumes that all the characters in the String are ASCII characters.

public static byte[] ToAsciiBuffer(this string instance)

Parameters

instance string

The string to convert.

Returns

byte[]

The string as ASCII encoded bytes.

See Also

ToCamelCase(string)

Returns the string converted to camelCase.

public static string ToCamelCase(this string instance)

Parameters

instance string

The string to convert.

Returns

string

The converted string.

ToFloat(string)

Converts a string, containing a decimal number, into a float.

public static float ToFloat(this string instance)

Parameters

instance string

The string to convert.

Returns

float

The number representation of the string.

See Also

ToInt(string)

Converts a string, containing an integer number, into an int.

public static int ToInt(this string instance)

Parameters

instance string

The string to convert.

Returns

int

The number representation of the string.

See Also

ToPascalCase(string)

Returns the string converted to PascalCase.

public static string ToPascalCase(this string instance)

Parameters

instance string

The string to convert.

Returns

string

The converted string.

ToSnakeCase(string)

Returns the string converted to snake_case.

public static string ToSnakeCase(this string instance)

Parameters

instance string

The string to convert.

Returns

string

The converted string.

ToUtf16Buffer(string)

Converts the string (which is an array of characters) to an UTF-16 encoded array of bytes.

public static byte[] ToUtf16Buffer(this string instance)

Parameters

instance string

The string to convert.

Returns

byte[]

The string as UTF-16 encoded bytes.

See Also

ToUtf32Buffer(string)

Converts the string (which is an array of characters) to an UTF-32 encoded array of bytes.

public static byte[] ToUtf32Buffer(this string instance)

Parameters

instance string

The string to convert.

Returns

byte[]

The string as UTF-32 encoded bytes.

See Also

ToUtf8Buffer(string)

Converts the string (which is an array of characters) to an UTF-8 encoded array of bytes. The conversion is a bit slower than ToAsciiBuffer(string), but supports all UTF-8 characters. Therefore, you should prefer this function over ToAsciiBuffer(string).

public static byte[] ToUtf8Buffer(this string instance)

Parameters

instance string

The string to convert.

Returns

byte[]

The string as UTF-8 encoded bytes.

See Also

TrimPrefix(string, string)

Removes a given string from the start if it starts with it or leaves the string unchanged.

public static string TrimPrefix(this string instance, string prefix)

Parameters

instance string

The string to remove the prefix from.

prefix string

The string to remove from the start.

Returns

string

A copy of the string with the prefix string removed from the start.

TrimSuffix(string, string)

Removes a given string from the end if it ends with it or leaves the string unchanged.

public static string TrimSuffix(this string instance, string suffix)

Parameters

instance string

The string to remove the suffix from.

suffix string

The string to remove from the end.

Returns

string

A copy of the string with the suffix string removed from the end.

URIDecode(string)

Decodes a string in URL encoded format. This is meant to decode parameters in a URL when receiving an HTTP request. This mostly wraps around UnescapeDataString(string), but also handles +. See URIEncode(string) for encoding.

public static string URIDecode(this string instance)

Parameters

instance string

The string to decode.

Returns

string

The unescaped string.

URIEncode(string)

Encodes a string to URL friendly format. This is meant to encode parameters in a URL when sending an HTTP request. This wraps around EscapeDataString(string). See URIDecode(string) for decoding.

public static string URIEncode(this string instance)

Parameters

instance string

The string to encode.

Returns

string

The escaped string.

ValidateNodeName(string)

Removes any characters from the string that are prohibited in Node names (.:@/").

public static string ValidateNodeName(this string instance)

Parameters

instance string

The string to sanitize.

Returns

string

The string sanitized as a valid node name.

XMLEscape(string)

Returns a copy of the string with special characters escaped using the XML standard.

public static string XMLEscape(this string instance)

Parameters

instance string

The string to escape.

Returns

string

The escaped string.

See Also

XMLUnescape(string)

Returns a copy of the string with escaped characters replaced by their meanings according to the XML standard.

public static string? XMLUnescape(this string instance)

Parameters

instance string

The string to unescape.

Returns

string

The unescaped string.

See Also