diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-11-02 13:18:34 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-02 13:18:34 +0100 |
commit | ab6ec9310c9dadb13b9a6acde529cfc8cc3ab086 (patch) | |
tree | face5718041ad50ab4bdb7681b840965e2c119ce | |
parent | 87727f70ed5316253407e4435a1ea1fb379d1b80 (diff) | |
parent | e5725c7debd025be9f7b475527324b4b934fed58 (diff) |
Merge pull request #54453 from KoBeWi/slice_of_string
-rw-r--r-- | core/variant/variant_call.cpp | 1 | ||||
-rw-r--r-- | doc/classes/String.xml | 14 |
2 files changed, 15 insertions, 0 deletions
diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index ec3a6a5ca8..2b1d330942 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1366,6 +1366,7 @@ static void _register_variant_builtin_methods() { bind_method(String, naturalnocasecmp_to, sarray("to"), varray()); bind_method(String, length, sarray(), varray()); bind_method(String, substr, sarray("from", "len"), varray(-1)); + bind_method(String, get_slice, sarray("delimiter", "slice"), varray()); bind_methodv(String, find, static_cast<int (String::*)(const String &, int) const>(&String::find), sarray("what", "from"), varray(0)); bind_method(String, count, sarray("what", "from", "to"), varray(0, 0)); bind_method(String, countn, sarray("what", "from", "to"), varray(0, 0)); diff --git a/doc/classes/String.xml b/doc/classes/String.xml index 1190d90190..a58bfd5c15 100644 --- a/doc/classes/String.xml +++ b/doc/classes/String.xml @@ -202,6 +202,19 @@ If the string is a valid file path, returns the filename. </description> </method> + <method name="get_slice" qualifiers="const"> + <return type="String" /> + <argument index="0" name="delimiter" type="String" /> + <argument index="1" name="slice" type="int" /> + <description> + Splits a string using a [code]delimiter[/code] and returns a substring at index [code]slice[/code]. Returns an empty string if the index doesn't exist. + This is a more performant alternative to [method split] for cases when you need only one element from the array at a fixed index. + Example: + [codeblock] + print("i/am/example/string".get_slice("/", 2)) # Prints 'example'. + [/codeblock] + </description> + </method> <method name="hash" qualifiers="const"> <return type="int" /> <description> @@ -602,6 +615,7 @@ <description> Splits the string by a [code]delimiter[/code] string and returns an array of the substrings. The [code]delimiter[/code] can be of any length. If [code]maxsplit[/code] is specified, it defines the number of splits to do from the left up to [code]maxsplit[/code]. The default value of [code]0[/code] means that all items are split. + If you need only one element from the array at a specific index, [method get_slice] is a more performant option. Example: [codeblocks] [gdscript] |