Debian Codegraph @

doc/classes/String.xml

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <class name="String" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3 <brief_description>
4 A built-in type for strings.
5 </brief_description>
6 <description>
7 This is the built-in string Variant type (and the one used by GDScript). Strings may contain any number of Unicode characters, and expose methods useful for manipulating and generating strings. Strings are reference-counted and use a copy-on-write approach (every modification to a string returns a new [String]), so passing them around is cheap in resources.
8 Some string methods have corresponding variations. Variations suffixed with [code]n[/code] ([method countn], [method findn], [method replacen], etc.) are [b]case-insensitive[/b] (they make no distinction between uppercase and lowercase letters). Method variations prefixed with [code]r[/code] ([method rfind], [method rsplit], etc.) are reversed, and start from the end of the string, instead of the beginning.
9 To convert any [Variant] to or from a string, see [method @GlobalScope.str], [method @GlobalScope.str_to_var], and [method @GlobalScope.var_to_str].
10 [b]Note:[/b] In a boolean context, a string will evaluate to [code]false[/code] if it is empty ([code]""[/code]). Otherwise, a string will always evaluate to [code]true[/code].
11 </description>
12 <tutorials>
13 <link title="GDScript format strings">$DOCS_URL/tutorials/scripting/gdscript/gdscript_format_string.html</link>
14 </tutorials>
15 <constructors>
16 <constructor name="String">
17 <return type="String" />
18 <description>
19 Constructs an empty [String] ([code]""[/code]).
20 </description>
21 </constructor>
22 <constructor name="String">
23 <return type="String" />
24 <param index="0" name="from" type="String" />
25 <description>
26 Constructs a [String] as a copy of the given [String].
27 </description>
28 </constructor>
29 <constructor name="String">
30 <return type="String" />
31 <param index="0" name="from" type="NodePath" />
32 <description>
33 Constructs a new [String] from the given [NodePath].
34 </description>
35 </constructor>
36 <constructor name="String">
37 <return type="String" />
38 <param index="0" name="from" type="StringName" />
39 <description>
40 Constructs a new [String] from the given [StringName].
41 </description>
42 </constructor>
43 </constructors>
44 <methods>
45 <method name="begins_with" qualifiers="const" keywords="starts_with">
46 <return type="bool" />
47 <param index="0" name="text" type="String" />
48 <description>
49 Returns [code]true[/code] if the string begins with the given [param text]. See also [method ends_with].
50 </description>
51 </method>
52 <method name="bigrams" qualifiers="const">
53 <return type="PackedStringArray" />
54 <description>
55 Returns an array containing the bigrams (pairs of consecutive characters) of this string.
56 [codeblock]
57 print("Get up!".bigrams()) # Prints ["Ge", "et", "t ", " u", "up", "p!"]
58 [/codeblock]
59 </description>
60 </method>
61 <method name="bin_to_int" qualifiers="const">
62 <return type="int" />
63 <description>
64 Converts the string representing a binary number into an [int]. The string may optionally be prefixed with [code]"0b"[/code], and an additional [code]-[/code] prefix for negative numbers.
65 [codeblocks]
66 [gdscript]
67 print("101".bin_to_int()) # Prints 5
68 print("0b101".bin_to_int()) # Prints 5
69 print("-0b10".bin_to_int()) # Prints -2
70 [/gdscript]
71 [csharp]
72 GD.Print("101".BinToInt()); // Prints 5
73 GD.Print("0b101".BinToInt()); // Prints 5
74 GD.Print("-0b10".BinToInt()); // Prints -2
75 [/csharp]
76 [/codeblocks]
77 </description>
78 </method>
79 <method name="c_escape" qualifiers="const">
80 <return type="String" />
81 <description>
82 Returns a copy of the string with special characters escaped using the C language standard.
83 </description>
84 </method>
85 <method name="c_unescape" qualifiers="const">
86 <return type="String" />
87 <description>
88 Returns a copy of the string with escaped characters replaced by their meanings. Supported escape sequences are [code]\'[/code], [code]\"[/code], [code]\\[/code], [code]\a[/code], [code]\b[/code], [code]\f[/code], [code]\n[/code], [code]\r[/code], [code]\t[/code], [code]\v[/code].
89 [b]Note:[/b] Unlike the GDScript parser, this method doesn't support the [code]\uXXXX[/code] escape sequence.
90 </description>
91 </method>
92 <method name="capitalize" qualifiers="const">
93 <return type="String" />
94 <description>
95 Changes the appearance of the string: replaces underscores ([code]_[/code]) 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.
96 [codeblocks]
97 [gdscript]
98 "move_local_x".capitalize() # Returns "Move Local X"
99 "sceneFile_path".capitalize() # Returns "Scene File Path"
100 "2D, FPS, PNG".capitalize() # Returns "2d, Fps, Png"
101 [/gdscript]
102 [csharp]
103 "move_local_x".Capitalize(); // Returns "Move Local X"
104 "sceneFile_path".Capitalize(); // Returns "Scene File Path"
105 "2D, FPS, PNG".Capitalize(); // Returns "2d, Fps, Png"
106 [/csharp]
107 [/codeblocks]
108 </description>
109 </method>
110 <method name="casecmp_to" qualifiers="const">
111 <return type="int" />
112 <param index="0" name="to" type="String" />
113 <description>
114 Performs a case-sensitive comparison to another string. Returns [code]-1[/code] if less than, [code]1[/code] if greater than, or [code]0[/code] if equal. "Less than" and "greater than" are determined by the [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode code points[/url] of each string, which roughly matches the alphabetical order.
115 If the character comparison reaches the end of one string, but the other string contains more characters, then it will use length as the deciding factor: [code]1[/code] will be returned if this string is longer than the [param to] string, or [code]-1[/code] if shorter. Note that the length of empty strings is always [code]0[/code].
116 To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method nocasecmp_to], [method filecasecmp_to], and [method naturalcasecmp_to].
117 </description>
118 </method>
119 <method name="chr" qualifiers="static">
120 <return type="String" />
121 <param index="0" name="code" type="int" />
122 <description>
123 Returns a single Unicode character from the integer [param code]. You may use [url=https://unicodelookup.com/]unicodelookup.com[/url] or [url=https://www.unicode.org/charts/]unicode.org[/url] as points of reference.
124 [codeblock]
125 print(String.chr(65)) # Prints "A"
126 print(String.chr(129302)) # Prints "🤖" (robot face emoji)
127 [/codeblock]
128 See also [method unicode_at], [method @GDScript.char], and [method @GDScript.ord].
129 </description>
130 </method>
131 <method name="contains" qualifiers="const">
132 <return type="bool" />
133 <param index="0" name="what" type="String" />
134 <description>
135 Returns [code]true[/code] if the string contains [param what]. In GDScript, this corresponds to the [code]in[/code] operator.
136 [codeblocks]
137 [gdscript]
138 print("Node".contains("de")) # Prints true
139 print("team".contains("I")) # Prints false
140 print("I" in "team") # Prints false
141 [/gdscript]
142 [csharp]
143 GD.Print("Node".Contains("de")); // Prints True
144 GD.Print("team".Contains("I")); // Prints False
145 [/csharp]
146 [/codeblocks]
147 If you need to know where [param what] is within the string, use [method find]. See also [method containsn].
148 </description>
149 </method>
150 <method name="containsn" qualifiers="const">
151 <return type="bool" />
152 <param index="0" name="what" type="String" />
153 <description>
154 Returns [code]true[/code] if the string contains [param what], [b]ignoring case[/b].
155 If you need to know where [param what] is within the string, use [method findn]. See also [method contains].
156 </description>
157 </method>
158 <method name="count" qualifiers="const">
159 <return type="int" />
160 <param index="0" name="what" type="String" />
161 <param index="1" name="from" type="int" default="0" />
162 <param index="2" name="to" type="int" default="0" />
163 <description>
164 Returns the number of occurrences of the substring [param what] between [param from] and [param to] positions. If [param to] is 0, the search continues until the end of the string.
165 </description>
166 </method>
167 <method name="countn" qualifiers="const">
168 <return type="int" />
169 <param index="0" name="what" type="String" />
170 <param index="1" name="from" type="int" default="0" />
171 <param index="2" name="to" type="int" default="0" />
172 <description>
173 Returns the number of occurrences of the substring [param what] between [param from] and [param to] positions, [b]ignoring case[/b]. If [param to] is 0, the search continues until the end of the string.
174 </description>
175 </method>
176 <method name="dedent" qualifiers="const">
177 <return type="String" />
178 <description>
179 Returns a copy of the string with indentation (leading tabs and spaces) removed. See also [method indent] to add indentation.
180 </description>
181 </method>
182 <method name="ends_with" qualifiers="const">
183 <return type="bool" />
184 <param index="0" name="text" type="String" />
185 <description>
186 Returns [code]true[/code] if the string ends with the given [param text]. See also [method begins_with].
187 </description>
188 </method>
189 <method name="erase" qualifiers="const">
190 <return type="String" />
191 <param index="0" name="position" type="int" />
192 <param index="1" name="chars" type="int" default="1" />
193 <description>
194 Returns a string with [param chars] characters erased starting from [param position]. If [param chars] goes beyond the string's length given the specified [param position], fewer characters will be erased from the returned string. Returns an empty string if either [param position] or [param chars] is negative. Returns the original string unmodified if [param chars] is [code]0[/code].
195 </description>
196 </method>
197 <method name="filecasecmp_to" qualifiers="const">
198 <return type="int" />
199 <param index="0" name="to" type="String" />
200 <description>
201 Like [method naturalcasecmp_to] but prioritizes strings that begin with periods ([code].[/code]) and underscores ([code]_[/code]) before any other character. Useful when sorting folders or file names.
202 To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method filenocasecmp_to], [method naturalcasecmp_to], and [method casecmp_to].
203 </description>
204 </method>
205 <method name="filenocasecmp_to" qualifiers="const">
206 <return type="int" />
207 <param index="0" name="to" type="String" />
208 <description>
209 Like [method naturalnocasecmp_to] but prioritizes strings that begin with periods ([code].[/code]) and underscores ([code]_[/code]) before any other character. Useful when sorting folders or file names.
210 To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method filecasecmp_to], [method naturalnocasecmp_to], and [method nocasecmp_to].
211 </description>
212 </method>
213 <method name="find" qualifiers="const">
214 <return type="int" />
215 <param index="0" name="what" type="String" />
216 <param index="1" name="from" type="int" default="0" />
217 <description>
218 Returns the index of the [b]first[/b] occurrence of [param what] in this string, or [code]-1[/code] if there are none. The search's start can be specified with [param from], continuing to the end of the string.
219 [codeblocks]
220 [gdscript]
221 print("Team".find("I")) # Prints -1
222
223 print("Potato".find("t")) # Prints 2
224 print("Potato".find("t", 3)) # Prints 4
225 print("Potato".find("t", 5)) # Prints -1
226 [/gdscript]
227 [csharp]
228 GD.Print("Team".Find("I")); // Prints -1
229
230 GD.Print("Potato".Find("t")); // Prints 2
231 GD.Print("Potato".Find("t", 3)); // Prints 4
232 GD.Print("Potato".Find("t", 5)); // Prints -1
233 [/csharp]
234 [/codeblocks]
235 [b]Note:[/b] If you just want to know whether the string contains [param what], use [method contains]. In GDScript, you may also use the [code]in[/code] operator.
236 [b]Note:[/b] A negative value of [param from] is converted to a starting index by counting back from the last possible index with enough space to find [param what].
237 </description>
238 </method>
239 <method name="findn" qualifiers="const">
240 <return type="int" />
241 <param index="0" name="what" type="String" />
242 <param index="1" name="from" type="int" default="0" />
243 <description>
244 Returns the index of the [b]first[/b] [b]case-insensitive[/b] occurrence of [param what] in this string, or [code]-1[/code] if there are none. The starting search index can be specified with [param from], continuing to the end of the string.
245 </description>
246 </method>
247 <method name="format" qualifiers="const">
248 <return type="String" />
249 <param index="0" name="values" type="Variant" />
250 <param index="1" name="placeholder" type="String" default="&quot;{_}&quot;" />
251 <description>
252 Formats the string by replacing all occurrences of [param placeholder] with the elements of [param values].
253 [param values] can be a [Dictionary], an [Array], or an [Object]. Any underscores in [param placeholder] will be replaced with the corresponding keys in advance. Array elements use their index as keys.
254 [codeblock]
255 # Prints "Waiting for Godot is a play by Samuel Beckett, and Godot Engine is named after it."
256 var use_array_values = "Waiting for {0} is a play by {1}, and {0} Engine is named after it."
257 print(use_array_values.format(["Godot", "Samuel Beckett"]))
258
259 # Prints "User 42 is Godot."
260 print("User {id} is {name}.".format({"id": 42, "name": "Godot"}))
261 [/codeblock]
262 Some additional handling is performed when [param values] is an [Array]. If [param placeholder] does not contain an underscore, the elements of the [param values] array will be used to replace one occurrence of the placeholder in order; If an element of [param values] is another 2-element array, it'll be interpreted as a key-value pair.
263 [codeblock]
264 # Prints "User 42 is Godot."
265 print("User {} is {}.".format([42, "Godot"], "{}"))
266 print("User {id} is {name}.".format([["id", 42], ["name", "Godot"]]))
267 [/codeblock]
268 When passing an [Object], the property names from [method Object.get_property_list] are used as keys.
269 [codeblock]
270 # Prints "Visible true, position (0, 0)"
271 var node = Node2D.new()
272 print("Visible {visible}, position {position}".format(node))
273 [/codeblock]
274 See also the [url=$DOCS_URL/tutorials/scripting/gdscript/gdscript_format_string.html]GDScript format string[/url] tutorial.
275 [b]Note:[/b] Each replacement is done sequentially for each element of [param values], [b]not[/b] all at once. This means that if any element is inserted and it contains another placeholder, it may be changed by the next replacement. While this can be very useful, it often causes unexpected results. If not necessary, make sure [param values]'s elements do not contain placeholders.
276 [codeblock]
277 print("{0} {1}".format(["{1}", "x"])) # Prints "x x"
278 print("{0} {1}".format(["x", "{0}"])) # Prints "x {0}"
279 print("{a} {b}".format({"a": "{b}", "b": "c"})) # Prints "c c"
280 print("{a} {b}".format({"b": "c", "a": "{b}"})) # Prints "{b} c"
281 [/codeblock]
282 [b]Note:[/b] In C#, it's recommended to [url=https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated]interpolate strings with "$"[/url], instead.
283 </description>
284 </method>
285 <method name="get_base_dir" qualifiers="const">
286 <return type="String" />
287 <description>
288 If the string is a valid file path, returns the base directory name.
289 [codeblock]
290 var dir_path = "/path/to/file.txt".get_base_dir() # dir_path is "/path/to"
291 [/codeblock]
292 </description>
293 </method>
294 <method name="get_basename" qualifiers="const">
295 <return type="String" />
296 <description>
297 If the string is a valid file path, returns the full file path, without the extension.
298 [codeblock]
299 var base = "/path/to/file.txt".get_basename() # base is "/path/to/file"
300 [/codeblock]
301 </description>
302 </method>
303 <method name="get_extension" qualifiers="const">
304 <return type="String" />
305 <description>
306 If the string is a valid file name or path, returns the file extension without the leading period ([code].[/code]). Otherwise, returns an empty string.
307 [codeblock]
308 var a = "/path/to/file.txt".get_extension() # a is "txt"
309 var b = "cool.txt".get_extension() # b is "txt"
310 var c = "cool.font.tres".get_extension() # c is "tres"
311 var d = ".pack1".get_extension() # d is "pack1"
312
313 var e = "file.txt.".get_extension() # e is ""
314 var f = "file.txt..".get_extension() # f is ""
315 var g = "txt".get_extension() # g is ""
316 var h = "".get_extension() # h is ""
317 [/codeblock]
318 </description>
319 </method>
320 <method name="get_file" qualifiers="const">
321 <return type="String" />
322 <description>
323 If the string is a valid file path, returns the file name, including the extension.
324 [codeblock]
325 var file = "/path/to/icon.png".get_file() # file is "icon.png"
326 [/codeblock]
327 </description>
328 </method>
329 <method name="get_slice" qualifiers="const">
330 <return type="String" />
331 <param index="0" name="delimiter" type="String" />
332 <param index="1" name="slice" type="int" />
333 <description>
334 Splits the string using a [param delimiter] and returns the substring at index [param slice]. Returns the original string if [param delimiter] does not occur in the string. Returns an empty string if the [param slice] does not exist.
335 This is faster than [method split], if you only need one or two substrings.
336 [codeblock]
337 print("i/am/example/hi".get_slice("/", 2)) # Prints "example"
338 [/codeblock]
339 </description>
340 </method>
341 <method name="get_slice_count" qualifiers="const">
342 <return type="int" />
343 <param index="0" name="delimiter" type="String" />
344 <description>
345 Returns the total number of slices when the string is split with the given [param delimiter] (see [method split]).
346 Use [method get_slice] to extract a specific slice.
347 [codeblock]
348 print("i/am/example/string".get_slice_count("/")) # Prints '4'.
349 print("i am example string".get_slice_count("/")) # Prints '1'.
350 [/codeblock]
351 </description>
352 </method>
353 <method name="get_slicec" qualifiers="const">
354 <return type="String" />
355 <param index="0" name="delimiter" type="int" />
356 <param index="1" name="slice" type="int" />
357 <description>
358 Splits the string using a Unicode character with code [param delimiter] and returns the substring at index [param slice]. Returns an empty string if the [param slice] does not exist.
359 This is faster than [method split], if you only need one or two substrings.
360 This is a Unicode version of [method get_slice].
361 </description>
362 </method>
363 <method name="hash" qualifiers="const">
364 <return type="int" />
365 <description>
366 Returns the 32-bit hash value representing the string's contents.
367 [b]Note:[/b] Strings with equal hash values are [i]not[/i] guaranteed to be the same, as a result of hash collisions. On the contrary, strings with different hash values are guaranteed to be different.
368 </description>
369 </method>
370 <method name="hex_decode" qualifiers="const">
371 <return type="PackedByteArray" />
372 <description>
373 Decodes a hexadecimal string as a [PackedByteArray].
374 [codeblocks]
375 [gdscript]
376 var text = "hello world"
377 var encoded = text.to_utf8_buffer().hex_encode() # outputs "68656c6c6f20776f726c64"
378 print(encoded.hex_decode().get_string_from_utf8())
379 [/gdscript]
380 [csharp]
381 var text = "hello world";
382 var encoded = text.ToUtf8Buffer().HexEncode(); // outputs "68656c6c6f20776f726c64"
383 GD.Print(encoded.HexDecode().GetStringFromUtf8());
384 [/csharp]
385 [/codeblocks]
386 </description>
387 </method>
388 <method name="hex_to_int" qualifiers="const">
389 <return type="int" />
390 <description>
391 Converts the string representing a hexadecimal number into an [int]. The string may be optionally prefixed with [code]"0x"[/code], and an additional [code]-[/code] prefix for negative numbers.
392 [codeblocks]
393 [gdscript]
394 print("0xff".hex_to_int()) # Prints 255
395 print("ab".hex_to_int()) # Prints 171
396 [/gdscript]
397 [csharp]
398 GD.Print("0xff".HexToInt()); // Prints 255
399 GD.Print("ab".HexToInt()); // Prints 171
400 [/csharp]
401 [/codeblocks]
402 </description>
403 </method>
404 <method name="humanize_size" qualifiers="static">
405 <return type="String" />
406 <param index="0" name="size" type="int" />
407 <description>
408 Converts [param size] which represents a number of bytes into a human-readable form.
409 The result is in [url=https://en.wikipedia.org/wiki/Binary_prefix#IEC_prefixes]IEC prefix format[/url], which may end in either [code]"B"[/code], [code]"KiB"[/code], [code]"MiB"[/code], [code]"GiB"[/code], [code]"TiB"[/code], [code]"PiB"[/code], or [code]"EiB"[/code].
410 </description>
411 </method>
412 <method name="indent" qualifiers="const">
413 <return type="String" />
414 <param index="0" name="prefix" type="String" />
415 <description>
416 Indents every line of the string with the given [param prefix]. Empty lines are not indented. See also [method dedent] to remove indentation.
417 For example, the string can be indented with two tabulations using [code]"\t\t"[/code], or four spaces using [code]" "[/code].
418 </description>
419 </method>
420 <method name="insert" qualifiers="const">
421 <return type="String" />
422 <param index="0" name="position" type="int" />
423 <param index="1" name="what" type="String" />
424 <description>
425 Inserts [param what] at the given [param position] in the string.
426 </description>
427 </method>
428 <method name="is_absolute_path" qualifiers="const">
429 <return type="bool" />
430 <description>
431 Returns [code]true[/code] if the string is a path to a file or directory, and its starting point is explicitly defined. This method is the opposite of [method is_relative_path].
432 This includes all paths starting with [code]"res://"[/code], [code]"user://"[/code], [code]"C:\"[/code], [code]"/"[/code], etc.
433 </description>
434 </method>
435 <method name="is_empty" qualifiers="const">
436 <return type="bool" />
437 <description>
438 Returns [code]true[/code] if the string's length is [code]0[/code] ([code]""[/code]). See also [method length].
439 </description>
440 </method>
441 <method name="is_relative_path" qualifiers="const">
442 <return type="bool" />
443 <description>
444 Returns [code]true[/code] if the string is a path, and its starting point is dependent on context. The path could begin from the current directory, or the current [Node] (if the string is derived from a [NodePath]), and may sometimes be prefixed with [code]"./"[/code]. This method is the opposite of [method is_absolute_path].
445 </description>
446 </method>
447 <method name="is_subsequence_of" qualifiers="const">
448 <return type="bool" />
449 <param index="0" name="text" type="String" />
450 <description>
451 Returns [code]true[/code] if all characters of this string can be found in [param text] in their original order. This is not the same as [method contains].
452 [codeblock]
453 var text = "Wow, incredible!"
454
455 print("inedible".is_subsequence_of(text)) # Prints true
456 print("Word!".is_subsequence_of(text)) # Prints true
457 print("Window".is_subsequence_of(text)) # Prints false
458 print("".is_subsequence_of(text)) # Prints true
459 [/codeblock]
460 </description>
461 </method>
462 <method name="is_subsequence_ofn" qualifiers="const">
463 <return type="bool" />
464 <param index="0" name="text" type="String" />
465 <description>
466 Returns [code]true[/code] if all characters of this string can be found in [param text] in their original order, [b]ignoring case[/b]. This is not the same as [method containsn].
467 </description>
468 </method>
469 <method name="is_valid_ascii_identifier" qualifiers="const">
470 <return type="bool" />
471 <description>
472 Returns [code]true[/code] if this string is a valid ASCII identifier. A valid ASCII identifier may contain only letters, digits, and underscores ([code]_[/code]), and the first character may not be a digit.
473 [codeblock]
474 print("node_2d".is_valid_ascii_identifier()) # Prints true
475 print("TYPE_FLOAT".is_valid_ascii_identifier()) # Prints true
476 print("1st_method".is_valid_ascii_identifier()) # Prints false
477 print("MyMethod#2".is_valid_ascii_identifier()) # Prints false
478 [/codeblock]
479 See also [method is_valid_unicode_identifier].
480 </description>
481 </method>
482 <method name="is_valid_filename" qualifiers="const">
483 <return type="bool" />
484 <description>
485 Returns [code]true[/code] if this string is a valid file name. A valid file name cannot be empty, begin or end with space characters, or contain characters that are not allowed ([code]:[/code] [code]/[/code] [code]\[/code] [code]?[/code] [code]*[/code] [code]"[/code] [code]|[/code] [code]%[/code] [code]&lt;[/code] [code]&gt;[/code]).
486 </description>
487 </method>
488 <method name="is_valid_float" qualifiers="const">
489 <return type="bool" />
490 <description>
491 Returns [code]true[/code] if this string represents a valid floating-point number. A valid float may contain only digits, one decimal point ([code].[/code]), and the exponent letter ([code]e[/code]). It may also be prefixed with a positive ([code]+[/code]) or negative ([code]-[/code]) sign. Any valid integer is also a valid float (see [method is_valid_int]). See also [method to_float].
492 [codeblock]
493 print("1.7".is_valid_float()) # Prints true
494 print("24".is_valid_float()) # Prints true
495 print("7e3".is_valid_float()) # Prints true
496 print("Hello".is_valid_float()) # Prints false
497 [/codeblock]
498 </description>
499 </method>
500 <method name="is_valid_hex_number" qualifiers="const">
501 <return type="bool" />
502 <param index="0" name="with_prefix" type="bool" default="false" />
503 <description>
504 Returns [code]true[/code] if this string is a valid hexadecimal number. A valid hexadecimal number only contains digits or letters [code]A[/code] to [code]F[/code] (either uppercase or lowercase), and may be prefixed with a positive ([code]+[/code]) or negative ([code]-[/code]) sign.
505 If [param with_prefix] is [code]true[/code], the hexadecimal number needs to prefixed by [code]"0x"[/code] to be considered valid.
506 [codeblock]
507 print("A08E".is_valid_hex_number()) # Prints true
508 print("-AbCdEf".is_valid_hex_number()) # Prints true
509 print("2.5".is_valid_hex_number()) # Prints false
510
511 print("0xDEADC0DE".is_valid_hex_number(true)) # Prints true
512 [/codeblock]
513 </description>
514 </method>
515 <method name="is_valid_html_color" qualifiers="const">
516 <return type="bool" />
517 <description>
518 Returns [code]true[/code] if this string is a valid color in hexadecimal HTML notation. The string must be a hexadecimal value (see [method is_valid_hex_number]) of either 3, 4, 6 or 8 digits, and may be prefixed by a hash sign ([code]#[/code]). Other HTML notations for colors, such as names or [code]hsl()[/code], are not considered valid. See also [method Color.html].
519 </description>
520 </method>
521 <method name="is_valid_identifier" qualifiers="const" deprecated="Use [method is_valid_ascii_identifier] instead.">
522 <return type="bool" />
523 <description>
524 Returns [code]true[/code] if this string is a valid identifier. A valid identifier may contain only letters, digits and underscores ([code]_[/code]), and the first character may not be a digit.
525 [codeblock]
526 print("node_2d".is_valid_identifier()) # Prints true
527 print("TYPE_FLOAT".is_valid_identifier()) # Prints true
528 print("1st_method".is_valid_identifier()) # Prints false
529 print("MyMethod#2".is_valid_identifier()) # Prints false
530 [/codeblock]
531 </description>
532 </method>
533 <method name="is_valid_int" qualifiers="const">
534 <return type="bool" />
535 <description>
536 Returns [code]true[/code] if this string represents a valid integer. A valid integer only contains digits, and may be prefixed with a positive ([code]+[/code]) or negative ([code]-[/code]) sign. See also [method to_int].
537 [codeblock]
538 print("7".is_valid_int()) # Prints true
539 print("1.65".is_valid_int()) # Prints false
540 print("Hi".is_valid_int()) # Prints false
541 print("+3".is_valid_int()) # Prints true
542 print("-12".is_valid_int()) # Prints true
543 [/codeblock]
544 </description>
545 </method>
546 <method name="is_valid_ip_address" qualifiers="const">
547 <return type="bool" />
548 <description>
549 Returns [code]true[/code] if this string represents a well-formatted IPv4 or IPv6 address. This method considers [url=https://en.wikipedia.org/wiki/Reserved_IP_addresses]reserved IP addresses[/url] such as [code]"0.0.0.0"[/code] and [code]"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"[/code] as valid.
550 </description>
551 </method>
552 <method name="is_valid_unicode_identifier" qualifiers="const">
553 <return type="bool" />
554 <description>
555 Returns [code]true[/code] if this string is a valid Unicode identifier.
556 A valid Unicode identifier must begin with a Unicode character of class [code]XID_Start[/code] or [code]"_"[/code], and may contain Unicode characters of class [code]XID_Continue[/code] in the other positions.
557 [codeblock]
558 print("node_2d".is_valid_unicode_identifier()) # Prints true
559 print("1st_method".is_valid_unicode_identifier()) # Prints false
560 print("MyMethod#2".is_valid_unicode_identifier()) # Prints false
561 print("állóképesség".is_valid_unicode_identifier()) # Prints true
562 print("выносливость".is_valid_unicode_identifier()) # Prints true
563 print("体力".is_valid_unicode_identifier()) # Prints true
564 [/codeblock]
565 See also [method is_valid_ascii_identifier].
566 [b]Note:[/b] This method checks identifiers the same way as GDScript. See [method TextServer.is_valid_identifier] for more advanced checks.
567 </description>
568 </method>
569 <method name="join" qualifiers="const">
570 <return type="String" />
571 <param index="0" name="parts" type="PackedStringArray" />
572 <description>
573 Returns the concatenation of [param parts]' elements, with each element separated by the string calling this method. This method is the opposite of [method split].
574 [codeblocks]
575 [gdscript]
576 var fruits = ["Apple", "Orange", "Pear", "Kiwi"]
577
578 print(", ".join(fruits)) # Prints "Apple, Orange, Pear, Kiwi"
579 print("---".join(fruits)) # Prints "Apple---Orange---Pear---Kiwi"
580 [/gdscript]
581 [csharp]
582 string[] fruits = ["Apple", "Orange", "Pear", "Kiwi"];
583
584 // In C#, this method is static.
585 GD.Print(string.Join(", ", fruits)); // Prints "Apple, Orange, Pear, Kiwi"
586 GD.Print(string.Join("---", fruits)); // Prints "Apple---Orange---Pear---Kiwi"
587 [/csharp]
588 [/codeblocks]
589 </description>
590 </method>
591 <method name="json_escape" qualifiers="const">
592 <return type="String" />
593 <description>
594 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 [method c_unescape] to unescape the string, if necessary.
595 </description>
596 </method>
597 <method name="left" qualifiers="const">
598 <return type="String" />
599 <param index="0" name="length" type="int" />
600 <description>
601 Returns the first [param length] characters from the beginning of the string. If [param length] is negative, strips the last [param length] characters from the string's end.
602 [codeblock]
603 print("Hello World!".left(3)) # Prints "Hel"
604 print("Hello World!".left(-4)) # Prints "Hello Wo"
605 [/codeblock]
606 </description>
607 </method>
608 <method name="length" qualifiers="const" keywords="size">
609 <return type="int" />
610 <description>
611 Returns the number of characters in the string. Empty strings ([code]""[/code]) always return [code]0[/code]. See also [method is_empty].
612 </description>
613 </method>
614 <method name="lpad" qualifiers="const">
615 <return type="String" />
616 <param index="0" name="min_length" type="int" />
617 <param index="1" name="character" type="String" default="&quot; &quot;" />
618 <description>
619 Formats the string to be at least [param min_length] long by adding [param character]s to the left of the string, if necessary. See also [method rpad].
620 </description>
621 </method>
622 <method name="lstrip" qualifiers="const">
623 <return type="String" />
624 <param index="0" name="chars" type="String" />
625 <description>
626 Removes a set of characters defined in [param chars] from the string's beginning. See also [method rstrip].
627 [b]Note:[/b] [param chars] is not a prefix. Use [method trim_prefix] to remove a single prefix, rather than a set of characters.
628 </description>
629 </method>
630 <method name="match" qualifiers="const">
631 <return type="bool" />
632 <param index="0" name="expr" type="String" />
633 <description>
634 Does a simple expression match (also called "glob" or "globbing"), where [code]*[/code] matches zero or more arbitrary characters and [code]?[/code] matches any single character except a period ([code].[/code]). An empty string or empty expression always evaluates to [code]false[/code].
635 </description>
636 </method>
637 <method name="matchn" qualifiers="const">
638 <return type="bool" />
639 <param index="0" name="expr" type="String" />
640 <description>
641 Does a simple [b]case-insensitive[/b] expression match, where [code]*[/code] matches zero or more arbitrary characters and [code]?[/code] matches any single character except a period ([code].[/code]). An empty string or empty expression always evaluates to [code]false[/code].
642 </description>
643 </method>
644 <method name="md5_buffer" qualifiers="const">
645 <return type="PackedByteArray" />
646 <description>
647 Returns the [url=https://en.wikipedia.org/wiki/MD5]MD5 hash[/url] of the string as a [PackedByteArray].
648 </description>
649 </method>
650 <method name="md5_text" qualifiers="const">
651 <return type="String" />
652 <description>
653 Returns the [url=https://en.wikipedia.org/wiki/MD5]MD5 hash[/url] of the string as another [String].
654 </description>
655 </method>
656 <method name="naturalcasecmp_to" qualifiers="const">
657 <return type="int" />
658 <param index="0" name="to" type="String" />
659 <description>
660 Performs a [b]case-sensitive[/b], [i]natural order[/i] comparison to another string. Returns [code]-1[/code] if less than, [code]1[/code] if greater than, or [code]0[/code] if equal. "Less than" or "greater than" are determined by the [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode code points[/url] of each string, which roughly matches the alphabetical order.
661 When used for sorting, natural order comparison orders sequences of numbers by the combined value of each digit as is often expected, instead of the single digit's value. A sorted sequence of numbered strings will be [code]["1", "2", "3", ...][/code], not [code]["1", "10", "2", "3", ...][/code].
662 If the character comparison reaches the end of one string, but the other string contains more characters, then it will use length as the deciding factor: [code]1[/code] will be returned if this string is longer than the [param to] string, or [code]-1[/code] if shorter. Note that the length of empty strings is always [code]0[/code].
663 To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method naturalnocasecmp_to], [method filecasecmp_to], and [method nocasecmp_to].
664 </description>
665 </method>
666 <method name="naturalnocasecmp_to" qualifiers="const">
667 <return type="int" />
668 <param index="0" name="to" type="String" />
669 <description>
670 Performs a [b]case-insensitive[/b], [i]natural order[/i] comparison to another string. Returns [code]-1[/code] if less than, [code]1[/code] if greater than, or [code]0[/code] if equal. "Less than" or "greater than" are determined by the [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode code points[/url] of each string, which roughly matches the alphabetical order. Internally, lowercase characters are converted to uppercase for the comparison.
671 When used for sorting, natural order comparison orders sequences of numbers by the combined value of each digit as is often expected, instead of the single digit's value. A sorted sequence of numbered strings will be [code]["1", "2", "3", ...][/code], not [code]["1", "10", "2", "3", ...][/code].
672 If the character comparison reaches the end of one string, but the other string contains more characters, then it will use length as the deciding factor: [code]1[/code] will be returned if this string is longer than the [param to] string, or [code]-1[/code] if shorter. Note that the length of empty strings is always [code]0[/code].
673 To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method naturalcasecmp_to], [method filenocasecmp_to], and [method casecmp_to].
674 </description>
675 </method>
676 <method name="nocasecmp_to" qualifiers="const">
677 <return type="int" />
678 <param index="0" name="to" type="String" />
679 <description>
680 Performs a [b]case-insensitive[/b] comparison to another string. Returns [code]-1[/code] if less than, [code]1[/code] if greater than, or [code]0[/code] if equal. "Less than" or "greater than" are determined by the [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode code points[/url] of each string, which roughly matches the alphabetical order. Internally, lowercase characters are converted to uppercase for the comparison.
681 If the character comparison reaches the end of one string, but the other string contains more characters, then it will use length as the deciding factor: [code]1[/code] will be returned if this string is longer than the [param to] string, or [code]-1[/code] if shorter. Note that the length of empty strings is always [code]0[/code].
682 To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method casecmp_to], [method filenocasecmp_to], and [method naturalnocasecmp_to].
683 </description>
684 </method>
685 <method name="num" qualifiers="static">
686 <return type="String" />
687 <param index="0" name="number" type="float" />
688 <param index="1" name="decimals" type="int" default="-1" />
689 <description>
690 Converts a [float] to a string representation of a decimal number, with the number of decimal places specified in [param decimals].
691 If [param decimals] is [code]-1[/code] as by default, the string representation may only have up to 14 significant digits, with digits before the decimal point having priority over digits after.
692 Trailing zeros are not included in the string. The last digit is rounded, not truncated.
693 [codeblock]
694 String.num(3.141593) # Returns "3.141593"
695 String.num(3.141593, 3) # Returns "3.142"
696 String.num(3.14159300) # Returns "3.141593"
697
698 # Here, the last digit will be rounded up,
699 # which reduces the total digit count, since trailing zeros are removed:
700 String.num(42.129999, 5) # Returns "42.13"
701
702 # If `decimals` is not specified, the maximum number of significant digits is 14:
703 String.num(-0.0000012345432123454321) # Returns "-0.00000123454321"
704 String.num(-10000.0000012345432123454321) # Returns "-10000.0000012345"
705 [/codeblock]
706 </description>
707 </method>
708 <method name="num_int64" qualifiers="static">
709 <return type="String" />
710 <param index="0" name="number" type="int" />
711 <param index="1" name="base" type="int" default="10" />
712 <param index="2" name="capitalize_hex" type="bool" default="false" />
713 <description>
714 Converts the given [param number] to a string representation, with the given [param base].
715 By default, [param base] is set to decimal ([code]10[/code]). Other common bases in programming include binary ([code]2[/code]), [url=https://en.wikipedia.org/wiki/Octal]octal[/url] ([code]8[/code]), hexadecimal ([code]16[/code]).
716 If [param capitalize_hex] is [code]true[/code], digits higher than 9 are represented in uppercase.
717 </description>
718 </method>
719 <method name="num_scientific" qualifiers="static">
720 <return type="String" />
721 <param index="0" name="number" type="float" />
722 <description>
723 Converts the given [param number] to a string representation, in scientific notation.
724 [codeblocks]
725 [gdscript]
726 var n = -5.2e8
727 print(n) # Prints -520000000
728 print(String.num_scientific(n)) # Prints -5.2e+08
729 [/gdscript]
730 [csharp]
731 // This method is not implemented in C#.
732 // Use `string.ToString()` with "e" to achieve similar results.
733 var n = -5.2e8f;
734 GD.Print(n); // Prints -520000000
735 GD.Print(n.ToString("e1")); // Prints -5.2e+008
736 [/csharp]
737 [/codeblocks]
738 [b]Note:[/b] In C#, this method is not implemented. To achieve similar results, see C#'s [url=https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings]Standard numeric format strings[/url].
739 </description>
740 </method>
741 <method name="num_uint64" qualifiers="static">
742 <return type="String" />
743 <param index="0" name="number" type="int" />
744 <param index="1" name="base" type="int" default="10" />
745 <param index="2" name="capitalize_hex" type="bool" default="false" />
746 <description>
747 Converts the given unsigned [int] to a string representation, with the given [param base].
748 By default, [param base] is set to decimal ([code]10[/code]). Other common bases in programming include binary ([code]2[/code]), [url=https://en.wikipedia.org/wiki/Octal]octal[/url] ([code]8[/code]), hexadecimal ([code]16[/code]).
749 If [param capitalize_hex] is [code]true[/code], digits higher than 9 are represented in uppercase.
750 </description>
751 </method>
752 <method name="pad_decimals" qualifiers="const">
753 <return type="String" />
754 <param index="0" name="digits" type="int" />
755 <description>
756 Formats the string representing a number to have an exact number of [param digits] [i]after[/i] the decimal point.
757 </description>
758 </method>
759 <method name="pad_zeros" qualifiers="const">
760 <return type="String" />
761 <param index="0" name="digits" type="int" />
762 <description>
763 Formats the string representing a number to have an exact number of [param digits] [i]before[/i] the decimal point.
764 </description>
765 </method>
766 <method name="path_join" qualifiers="const">
767 <return type="String" />
768 <param index="0" name="path" type="String" />
769 <description>
770 Concatenates [param path] at the end of the string as a subpath, adding [code]/[/code] if necessary.
771 [b]Example:[/b] [code]"this/is".path_join("path") == "this/is/path"[/code].
772 </description>
773 </method>
774 <method name="remove_char" qualifiers="const">
775 <return type="String" />
776 <param index="0" name="what" type="int" />
777 <description>
778 Removes all occurrences of the Unicode character with code [param what]. Faster version of [method replace] when the key is only one character long and the replacement is [code]""[/code].
779 </description>
780 </method>
781 <method name="remove_chars" qualifiers="const">
782 <return type="String" />
783 <param index="0" name="chars" type="String" />
784 <description>
785 Removes all occurrences of the characters in [param chars]. See also [method remove_char].
786 </description>
787 </method>
788 <method name="repeat" qualifiers="const">
789 <return type="String" />
790 <param index="0" name="count" type="int" />
791 <description>
792 Repeats this string a number of times. [param count] needs to be greater than [code]0[/code]. Otherwise, returns an empty string.
793 </description>
794 </method>
795 <method name="replace" qualifiers="const">
796 <return type="String" />
797 <param index="0" name="what" type="String" />
798 <param index="1" name="forwhat" type="String" />
799 <description>
800 Replaces all occurrences of [param what] inside the string with the given [param forwhat].
801 </description>
802 </method>
803 <method name="replace_char" qualifiers="const">
804 <return type="String" />
805 <param index="0" name="key" type="int" />
806 <param index="1" name="with" type="int" />
807 <description>
808 Replaces all occurrences of the Unicode character with code [param key] with the Unicode character with code [param with]. Faster version of [method replace] when the key is only one character long. To get a single character use [code]"X".unicode_at(0)[/code] (note that some strings, like compound letters and emoji, can be composed of multiple unicode codepoints, and will not work with this method, use [method length] to make sure).
809 </description>
810 </method>
811 <method name="replace_chars" qualifiers="const">
812 <return type="String" />
813 <param index="0" name="keys" type="String" />
814 <param index="1" name="with" type="int" />
815 <description>
816 Replaces any occurrence of the characters in [param keys] with the Unicode character with code [param with]. See also [method replace_char].
817 </description>
818 </method>
819 <method name="replacen" qualifiers="const">
820 <return type="String" />
821 <param index="0" name="what" type="String" />
822 <param index="1" name="forwhat" type="String" />
823 <description>
824 Replaces all [b]case-insensitive[/b] occurrences of [param what] inside the string with the given [param forwhat].
825 </description>
826 </method>
827 <method name="reverse" qualifiers="const">
828 <return type="String" />
829 <description>
830 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.
831 </description>
832 </method>
833 <method name="rfind" qualifiers="const">
834 <return type="int" />
835 <param index="0" name="what" type="String" />
836 <param index="1" name="from" type="int" default="-1" />
837 <description>
838 Returns the index of the [b]last[/b] occurrence of [param what] in this string, or [code]-1[/code] if there are none. The search's start can be specified with [param from], continuing to the beginning of the string. This method is the reverse of [method find].
839 [b]Note:[/b] A negative value of [param from] is converted to a starting index by counting back from the last possible index with enough space to find [param what].
840 [b]Note:[/b] A value of [param from] that is greater than the last possible index with enough space to find [param what] is considered out-of-bounds, and returns [code]-1[/code].
841 </description>
842 </method>
843 <method name="rfindn" qualifiers="const">
844 <return type="int" />
845 <param index="0" name="what" type="String" />
846 <param index="1" name="from" type="int" default="-1" />
847 <description>
848 Returns the index of the [b]last[/b] [b]case-insensitive[/b] occurrence of [param what] in this string, or [code]-1[/code] if there are none. The starting search index can be specified with [param from], continuing to the beginning of the string. This method is the reverse of [method findn].
849 </description>
850 </method>
851 <method name="right" qualifiers="const">
852 <return type="String" />
853 <param index="0" name="length" type="int" />
854 <description>
855 Returns the last [param length] characters from the end of the string. If [param length] is negative, strips the first [param length] characters from the string's beginning.
856 [codeblock]
857 print("Hello World!".right(3)) # Prints "ld!"
858 print("Hello World!".right(-4)) # Prints "o World!"
859 [/codeblock]
860 </description>
861 </method>
862 <method name="rpad" qualifiers="const">
863 <return type="String" />
864 <param index="0" name="min_length" type="int" />
865 <param index="1" name="character" type="String" default="&quot; &quot;" />
866 <description>
867 Formats the string to be at least [param min_length] long, by adding [param character]s to the right of the string, if necessary. See also [method lpad].
868 </description>
869 </method>
870 <method name="rsplit" qualifiers="const">
871 <return type="PackedStringArray" />
872 <param index="0" name="delimiter" type="String" default="&quot;&quot;" />
873 <param index="1" name="allow_empty" type="bool" default="true" />
874 <param index="2" name="maxsplit" type="int" default="0" />
875 <description>
876 Splits the string using a [param 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. If [param delimiter] is an empty string, each substring will be a single character.
877 If [param allow_empty] is [code]false[/code], empty strings between adjacent delimiters are excluded from the array.
878 If [param maxsplit] is greater than [code]0[/code], the number of splits may not exceed [param maxsplit]. By default, the entire string is split, which is mostly identical to [method split].
879 [codeblocks]
880 [gdscript]
881 var some_string = "One,Two,Three,Four"
882 var some_array = some_string.rsplit(",", true, 1)
883
884 print(some_array.size()) # Prints 2
885 print(some_array[0]) # Prints "One,Two,Three"
886 print(some_array[1]) # Prints "Four"
887 [/gdscript]
888 [csharp]
889 // In C#, there is no String.RSplit() method.
890 [/csharp]
891 [/codeblocks]
892 </description>
893 </method>
894 <method name="rstrip" qualifiers="const">
895 <return type="String" />
896 <param index="0" name="chars" type="String" />
897 <description>
898 Removes a set of characters defined in [param chars] from the string's end. See also [method lstrip].
899 [b]Note:[/b] [param chars] is not a suffix. Use [method trim_suffix] to remove a single suffix, rather than a set of characters.
900 </description>
901 </method>
902 <method name="sha1_buffer" qualifiers="const">
903 <return type="PackedByteArray" />
904 <description>
905 Returns the [url=https://en.wikipedia.org/wiki/SHA-1]SHA-1[/url] hash of the string as a [PackedByteArray].
906 </description>
907 </method>
908 <method name="sha1_text" qualifiers="const">
909 <return type="String" />
910 <description>
911 Returns the [url=https://en.wikipedia.org/wiki/SHA-1]SHA-1[/url] hash of the string as another [String].
912 </description>
913 </method>
914 <method name="sha256_buffer" qualifiers="const">
915 <return type="PackedByteArray" />
916 <description>
917 Returns the [url=https://en.wikipedia.org/wiki/SHA-2]SHA-256[/url] hash of the string as a [PackedByteArray].
918 </description>
919 </method>
920 <method name="sha256_text" qualifiers="const">
921 <return type="String" />
922 <description>
923 Returns the [url=https://en.wikipedia.org/wiki/SHA-2]SHA-256[/url] hash of the string as another [String].
924 </description>
925 </method>
926 <method name="similarity" qualifiers="const">
927 <return type="float" />
928 <param index="0" name="text" type="String" />
929 <description>
930 Returns the similarity index ([url=https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient]Sørensen-Dice coefficient[/url]) of this string compared to another. A result of [code]1.0[/code] means totally similar, while [code]0.0[/code] means totally dissimilar.
931 [codeblock]
932 print("ABC123".similarity("ABC123")) # Prints 1.0
933 print("ABC123".similarity("XYZ456")) # Prints 0.0
934 print("ABC123".similarity("123ABC")) # Prints 0.8
935 print("ABC123".similarity("abc123")) # Prints 0.4
936 [/codeblock]
937 </description>
938 </method>
939 <method name="simplify_path" qualifiers="const">
940 <return type="String" />
941 <description>
942 If the string is a valid file path, converts the string into a canonical path. This is the shortest possible path, without [code]"./"[/code], and all the unnecessary [code]".."[/code] and [code]"/"[/code].
943 [codeblock]
944 var simple_path = "./path/to///../file".simplify_path()
945 print(simple_path) # Prints "path/file"
946 [/codeblock]
947 </description>
948 </method>
949 <method name="split" qualifiers="const">
950 <return type="PackedStringArray" />
951 <param index="0" name="delimiter" type="String" default="&quot;&quot;" />
952 <param index="1" name="allow_empty" type="bool" default="true" />
953 <param index="2" name="maxsplit" type="int" default="0" />
954 <description>
955 Splits the string using a [param delimiter] and returns an array of the substrings. If [param delimiter] is an empty string, each substring will be a single character. This method is the opposite of [method join].
956 If [param allow_empty] is [code]false[/code], empty strings between adjacent delimiters are excluded from the array.
957 If [param maxsplit] is greater than [code]0[/code], the number of splits may not exceed [param maxsplit]. By default, the entire string is split.
958 [codeblocks]
959 [gdscript]
960 var some_array = "One,Two,Three,Four".split(",", true, 2)
961
962 print(some_array.size()) # Prints 3
963 print(some_array[0]) # Prints "One"
964 print(some_array[1]) # Prints "Two"
965 print(some_array[2]) # Prints "Three,Four"
966 [/gdscript]
967 [csharp]
968 // C#'s `Split()` does not support the `maxsplit` parameter.
969 var someArray = "One,Two,Three".Split(",");
970
971 GD.Print(someArray[0]); // Prints "One"
972 GD.Print(someArray[1]); // Prints "Two"
973 GD.Print(someArray[2]); // Prints "Three"
974 [/csharp]
975 [/codeblocks]
976 [b]Note:[/b] If you only need one substring from the array, consider using [method get_slice] which is faster. If you need to split strings with more complex rules, use the [RegEx] class instead.
977 </description>
978 </method>
979 <method name="split_floats" qualifiers="const">
980 <return type="PackedFloat64Array" />
981 <param index="0" name="delimiter" type="String" />
982 <param index="1" name="allow_empty" type="bool" default="true" />
983 <description>
984 Splits the string into floats by using a [param delimiter] and returns a [PackedFloat64Array].
985 If [param allow_empty] is [code]false[/code], empty or invalid [float] conversions between adjacent delimiters are excluded.
986 [codeblock]
987 var a = "1,2,4.5".split_floats(",") # a is [1.0, 2.0, 4.5]
988 var c = "1| ||4.5".split_floats("|") # c is [1.0, 0.0, 0.0, 4.5]
989 var b = "1| ||4.5".split_floats("|", false) # b is [1.0, 4.5]
990 [/codeblock]
991 </description>
992 </method>
993 <method name="strip_edges" qualifiers="const">
994 <return type="String" />
995 <param index="0" name="left" type="bool" default="true" />
996 <param index="1" name="right" type="bool" default="true" />
997 <description>
998 Strips all non-printable characters from the beginning and the end of the string. These include spaces, tabulations ([code]\t[/code]), and newlines ([code]\n[/code] [code]\r[/code]).
999 If [param left] is [code]false[/code], ignores the string's beginning. Likewise, if [param right] is [code]false[/code], ignores the string's end.
1000 </description>
1001 </method>
1002 <method name="strip_escapes" qualifiers="const">
1003 <return type="String" />
1004 <description>
1005 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 ([code]\t[/code]) and newline ([code]\n[/code], [code]\r[/code]) characters, but [i]not[/i] spaces.
1006 </description>
1007 </method>
1008 <method name="substr" qualifiers="const">
1009 <return type="String" />
1010 <param index="0" name="from" type="int" />
1011 <param index="1" name="len" type="int" default="-1" />
1012 <description>
1013 Returns part of the string from the position [param from] with length [param len]. If [param len] is [code]-1[/code] (as by default), returns the rest of the string starting from the given position.
1014 </description>
1015 </method>
1016 <method name="to_ascii_buffer" qualifiers="const">
1017 <return type="PackedByteArray" />
1018 <description>
1019 Converts the string to an [url=https://en.wikipedia.org/wiki/ASCII]ASCII[/url]/Latin-1 encoded [PackedByteArray]. This method is slightly faster than [method to_utf8_buffer], but replaces all unsupported characters with spaces. This is the inverse of [method PackedByteArray.get_string_from_ascii].
1020 </description>
1021 </method>
1022 <method name="to_camel_case" qualifiers="const">
1023 <return type="String" />
1024 <description>
1025 Returns the string converted to [code]camelCase[/code].
1026 </description>
1027 </method>
1028 <method name="to_float" qualifiers="const">
1029 <return type="float" />
1030 <description>
1031 Converts the string representing a decimal number into a [float]. This method stops on the first non-number character, except the first decimal point ([code].[/code]) and the exponent letter ([code]e[/code]). See also [method is_valid_float].
1032 [codeblock]
1033 var a = "12.35".to_float() # a is 12.35
1034 var b = "1.2.3".to_float() # b is 1.2
1035 var c = "12xy3".to_float() # c is 12.0
1036 var d = "1e3".to_float() # d is 1000.0
1037 var e = "Hello!".to_float() # e is 0.0
1038 [/codeblock]
1039 </description>
1040 </method>
1041 <method name="to_int" qualifiers="const">
1042 <return type="int" />
1043 <description>
1044 Converts the string representing an integer number into an [int]. This method removes any non-number character and stops at the first decimal point ([code].[/code]). See also [method is_valid_int].
1045 [codeblock]
1046 var a = "123".to_int() # a is 123
1047 var b = "x1y2z3".to_int() # b is 123
1048 var c = "-1.2.3".to_int() # c is -1
1049 var d = "Hello!".to_int() # d is 0
1050 [/codeblock]
1051 </description>
1052 </method>
1053 <method name="to_kebab_case" qualifiers="const">
1054 <return type="String" />
1055 <description>
1056 Returns the string converted to [code]kebab-case[/code].
1057 [b]Note:[/b] Numbers followed by a [i]single[/i] letter are not separated in the conversion to keep some words (such as "2D") together.
1058 [codeblocks]
1059 [gdscript]
1060 "Node2D".to_kebab_case() # Returns "node-2d"
1061 "2nd place".to_kebab_case() # Returns "2-nd-place"
1062 "Texture3DAssetFolder".to_kebab_case() # Returns "texture-3d-asset-folder"
1063 [/gdscript]
1064 [csharp]
1065 "Node2D".ToKebabCase(); // Returns "node-2d"
1066 "2nd place".ToKebabCase(); // Returns "2-nd-place"
1067 "Texture3DAssetFolder".ToKebabCase(); // Returns "texture-3d-asset-folder"
1068 [/csharp]
1069 [/codeblocks]
1070 </description>
1071 </method>
1072 <method name="to_lower" qualifiers="const">
1073 <return type="String" />
1074 <description>
1075 Returns the string converted to [code]lowercase[/code].
1076 </description>
1077 </method>
1078 <method name="to_multibyte_char_buffer" qualifiers="const">
1079 <return type="PackedByteArray" />
1080 <param index="0" name="encoding" type="String" default="&quot;&quot;" />
1081 <description>
1082 Converts the string to system multibyte code page encoded [PackedByteArray]. If conversion fails, empty array is returned.
1083 The values permitted for [param encoding] are system dependent. If [param encoding] is empty string, system default encoding is used.
1084 - For Windows, see [url=https://learn.microsoft.com/en-us/windows/win32/Intl/code-page-identifiers]Code Page Identifiers[/url] .NET names.
1085 - For macOS and Linux/BSD, see [code]libiconv[/code] library documentation and [code]iconv --list[/code] for a list of supported encodings.
1086 </description>
1087 </method>
1088 <method name="to_pascal_case" qualifiers="const">
1089 <return type="String" />
1090 <description>
1091 Returns the string converted to [code]PascalCase[/code].
1092 </description>
1093 </method>
1094 <method name="to_snake_case" qualifiers="const">
1095 <return type="String" />
1096 <description>
1097 Returns the string converted to [code]snake_case[/code].
1098 [b]Note:[/b] Numbers followed by a [i]single[/i] letter are not separated in the conversion to keep some words (such as "2D") together.
1099 [codeblocks]
1100 [gdscript]
1101 "Node2D".to_snake_case() # Returns "node_2d"
1102 "2nd place".to_snake_case() # Returns "2_nd_place"
1103 "Texture3DAssetFolder".to_snake_case() # Returns "texture_3d_asset_folder"
1104 [/gdscript]
1105 [csharp]
1106 "Node2D".ToSnakeCase(); // Returns "node_2d"
1107 "2nd place".ToSnakeCase(); // Returns "2_nd_place"
1108 "Texture3DAssetFolder".ToSnakeCase(); // Returns "texture_3d_asset_folder"
1109 [/csharp]
1110 [/codeblocks]
1111 </description>
1112 </method>
1113 <method name="to_upper" qualifiers="const">
1114 <return type="String" />
1115 <description>
1116 Returns the string converted to [code]UPPERCASE[/code].
1117 </description>
1118 </method>
1119 <method name="to_utf8_buffer" qualifiers="const">
1120 <return type="PackedByteArray" />
1121 <description>
1122 Converts the string to a [url=https://en.wikipedia.org/wiki/UTF-8]UTF-8[/url] encoded [PackedByteArray]. This method is slightly slower than [method to_ascii_buffer], but supports all UTF-8 characters. For most cases, prefer using this method. This is the inverse of [method PackedByteArray.get_string_from_utf8].
1123 </description>
1124 </method>
1125 <method name="to_utf16_buffer" qualifiers="const">
1126 <return type="PackedByteArray" />
1127 <description>
1128 Converts the string to a [url=https://en.wikipedia.org/wiki/UTF-16]UTF-16[/url] encoded [PackedByteArray]. This is the inverse of [method PackedByteArray.get_string_from_utf16].
1129 </description>
1130 </method>
1131 <method name="to_utf32_buffer" qualifiers="const">
1132 <return type="PackedByteArray" />
1133 <description>
1134 Converts the string to a [url=https://en.wikipedia.org/wiki/UTF-32]UTF-32[/url] encoded [PackedByteArray]. This is the inverse of [method PackedByteArray.get_string_from_utf32].
1135 </description>
1136 </method>
1137 <method name="to_wchar_buffer" qualifiers="const">
1138 <return type="PackedByteArray" />
1139 <description>
1140 Converts the string to a [url=https://en.wikipedia.org/wiki/Wide_character]wide character[/url] ([code]wchar_t[/code], UTF-16 on Windows, UTF-32 on other platforms) encoded [PackedByteArray]. This is the inverse of [method PackedByteArray.get_string_from_wchar].
1141 </description>
1142 </method>
1143 <method name="trim_prefix" qualifiers="const">
1144 <return type="String" />
1145 <param index="0" name="prefix" type="String" />
1146 <description>
1147 Removes the given [param prefix] from the start of the string, or returns the string unchanged.
1148 </description>
1149 </method>
1150 <method name="trim_suffix" qualifiers="const">
1151 <return type="String" />
1152 <param index="0" name="suffix" type="String" />
1153 <description>
1154 Removes the given [param suffix] from the end of the string, or returns the string unchanged.
1155 </description>
1156 </method>
1157 <method name="unicode_at" qualifiers="const">
1158 <return type="int" />
1159 <param index="0" name="at" type="int" />
1160 <description>
1161 Returns the character code at position [param at].
1162 See also [method chr], [method @GDScript.char], and [method @GDScript.ord].
1163 </description>
1164 </method>
1165 <method name="uri_decode" qualifiers="const">
1166 <return type="String" />
1167 <description>
1168 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. See also [method uri_encode].
1169 [codeblocks]
1170 [gdscript]
1171 var url = "$DOCS_URL/?highlight=Godot%20Engine%3%docs"
1172 print(url.uri_decode()) # Prints "$DOCS_URL/?highlight=Godot Engine:docs"
1173 [/gdscript]
1174 [csharp]
1175 var url = "$DOCS_URL/?highlight=Godot%20Engine%3%docs"
1176 GD.Print(url.URIDecode()) // Prints "$DOCS_URL/?highlight=Godot Engine:docs"
1177 [/csharp]
1178 [/codeblocks]
1179 [b]Note:[/b] This method decodes [code]+[/code] as space.
1180 </description>
1181 </method>
1182 <method name="uri_encode" qualifiers="const">
1183 <return type="String" />
1184 <description>
1185 Encodes the string to URL-friendly format. This method is meant to properly encode the parameters in a URL when sending an HTTP request. See also [method uri_decode].
1186 [codeblocks]
1187 [gdscript]
1188 var prefix = "$DOCS_URL/?highlight="
1189 var url = prefix + "Godot Engine:docs".uri_encode()
1190
1191 print(url) # Prints "$DOCS_URL/?highlight=Godot%20Engine%3%docs"
1192 [/gdscript]
1193 [csharp]
1194 var prefix = "$DOCS_URL/?highlight=";
1195 var url = prefix + "Godot Engine:docs".URIEncode();
1196
1197 GD.Print(url); // Prints "$DOCS_URL/?highlight=Godot%20Engine%3%docs"
1198 [/csharp]
1199 [/codeblocks]
1200 </description>
1201 </method>
1202 <method name="uri_file_decode" qualifiers="const">
1203 <return type="String" />
1204 <description>
1205 Decodes the file path from its URL-encoded format. Unlike [method uri_decode] this method leaves [code]+[/code] as is.
1206 </description>
1207 </method>
1208 <method name="validate_filename" qualifiers="const">
1209 <return type="String" />
1210 <description>
1211 Returns a copy of the string with all characters that are not allowed in [method is_valid_filename] replaced with underscores.
1212 </description>
1213 </method>
1214 <method name="validate_node_name" qualifiers="const">
1215 <return type="String" />
1216 <description>
1217 Returns a copy of the string with all characters that are not allowed in [member Node.name] ([code].[/code] [code]:[/code] [code]@[/code] [code]/[/code] [code]"[/code] [code]%[/code]) replaced with underscores.
1218 </description>
1219 </method>
1220 <method name="xml_escape" qualifiers="const">
1221 <return type="String" />
1222 <param index="0" name="escape_quotes" type="bool" default="false" />
1223 <description>
1224 Returns a copy of the string with special characters escaped using the XML standard. If [param escape_quotes] is [code]true[/code], the single quote ([code]'[/code]) and double quote ([code]"[/code]) characters are also escaped.
1225 </description>
1226 </method>
1227 <method name="xml_unescape" qualifiers="const">
1228 <return type="String" />
1229 <description>
1230 Returns a copy of the string with escaped characters replaced by their meanings according to the XML standard.
1231 </description>
1232 </method>
1233 </methods>
1234 <operators>
1235 <operator name="operator !=">
1236 <return type="bool" />
1237 <param index="0" name="right" type="String" />
1238 <description>
1239 Returns [code]true[/code] if both strings do not contain the same sequence of characters.
1240 </description>
1241 </operator>
1242 <operator name="operator !=">
1243 <return type="bool" />
1244 <param index="0" name="right" type="StringName" />
1245 <description>
1246 Returns [code]true[/code] if this [String] is not equivalent to the given [StringName].
1247 </description>
1248 </operator>
1249 <operator name="operator %">
1250 <return type="String" />
1251 <param index="0" name="right" type="Variant" />
1252 <description>
1253 Formats the [String], replacing the placeholders with one or more parameters. To pass multiple parameters, [param right] needs to be an [Array].
1254 [codeblock]
1255 print("I caught %d fishes!" % 2) # Prints "I caught 2 fishes!"
1256
1257 var my_message = "Travelling to %s, at %2.2f km/h."
1258 var location = "Deep Valley"
1259 var speed = 40.3485
1260 print(my_message % [location, speed]) # Prints "Travelling to Deep Valley, at 40.35 km/h."
1261 [/codeblock]
1262 For more information, see the [url=$DOCS_URL/tutorials/scripting/gdscript/gdscript_format_string.html]GDScript format strings[/url] tutorial.
1263 [b]Note:[/b] In C#, this operator is not available. Instead, see [url=https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated]how to interpolate strings with "$"[/url].
1264 </description>
1265 </operator>
1266 <operator name="operator +">
1267 <return type="String" />
1268 <param index="0" name="right" type="String" />
1269 <description>
1270 Appends [param right] at the end of this [String], also known as a string concatenation.
1271 </description>
1272 </operator>
1273 <operator name="operator +">
1274 <return type="String" />
1275 <param index="0" name="right" type="StringName" />
1276 <description>
1277 Appends [param right] at the end of this [String], returning a [String]. This is also known as a string concatenation.
1278 </description>
1279 </operator>
1280 <operator name="operator &lt;">
1281 <return type="bool" />
1282 <param index="0" name="right" type="String" />
1283 <description>
1284 Returns [code]true[/code] if the left [String] comes before [param right] in [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode order[/url], which roughly matches the alphabetical order. Useful for sorting.
1285 </description>
1286 </operator>
1287 <operator name="operator &lt;=">
1288 <return type="bool" />
1289 <param index="0" name="right" type="String" />
1290 <description>
1291 Returns [code]true[/code] if the left [String] comes before [param right] in [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode order[/url], which roughly matches the alphabetical order, or if both are equal.
1292 </description>
1293 </operator>
1294 <operator name="operator ==">
1295 <return type="bool" />
1296 <param index="0" name="right" type="String" />
1297 <description>
1298 Returns [code]true[/code] if both strings contain the same sequence of characters.
1299 </description>
1300 </operator>
1301 <operator name="operator ==">
1302 <return type="bool" />
1303 <param index="0" name="right" type="StringName" />
1304 <description>
1305 Returns [code]true[/code] if this [String] is equivalent to the given [StringName].
1306 </description>
1307 </operator>
1308 <operator name="operator &gt;">
1309 <return type="bool" />
1310 <param index="0" name="right" type="String" />
1311 <description>
1312 Returns [code]true[/code] if the left [String] comes after [param right] in [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode order[/url], which roughly matches the alphabetical order. Useful for sorting.
1313 </description>
1314 </operator>
1315 <operator name="operator &gt;=">
1316 <return type="bool" />
1317 <param index="0" name="right" type="String" />
1318 <description>
1319 Returns [code]true[/code] if the left [String] comes after [param right] in [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode order[/url], which roughly matches the alphabetical order, or if both are equal.
1320 </description>
1321 </operator>
1322 <operator name="operator []">
1323 <return type="String" />
1324 <param index="0" name="index" type="int" />
1325 <description>
1326 Returns a new [String] that only contains the character at [param index]. Indices start from [code]0[/code]. If [param index] is greater or equal to [code]0[/code], the character is fetched starting from the beginning of the string. If [param index] is a negative value, it is fetched starting from the end. Accessing a string out-of-bounds will cause a run-time error, pausing the project execution if run from the editor.
1327 </description>
1328 </operator>
1329 </operators>
1330 </class>