Browse Source

Expose String.get_slice

kobewi 3 years ago
parent
commit
e5725c7deb
2 changed files with 15 additions and 0 deletions
  1. 1 0
      core/variant/variant_call.cpp
  2. 14 0
      doc/classes/String.xml

+ 1 - 0
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, naturalnocasecmp_to, sarray("to"), varray());
 	bind_method(String, length, sarray(), varray());
 	bind_method(String, length, sarray(), varray());
 	bind_method(String, substr, sarray("from", "len"), varray(-1));
 	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_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, count, sarray("what", "from", "to"), varray(0, 0));
 	bind_method(String, countn, sarray("what", "from", "to"), varray(0, 0));
 	bind_method(String, countn, sarray("what", "from", "to"), varray(0, 0));

+ 14 - 0
doc/classes/String.xml

@@ -200,6 +200,19 @@
 				If the string is a valid file path, returns the filename.
 				If the string is a valid file path, returns the filename.
 			</description>
 			</description>
 		</method>
 		</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">
 		<method name="hash" qualifiers="const">
 			<return type="int" />
 			<return type="int" />
 			<description>
 			<description>
@@ -676,6 +689,7 @@
 			<description>
 			<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.
 				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 [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:
 				Example:
 				[codeblocks]
 				[codeblocks]
 				[gdscript]
 				[gdscript]