Expose String.get_slice

(cherry picked from commit e5725c7deb)
This commit is contained in:
kobewi 2021-10-31 15:36:16 +01:00 committed by Rémi Verschelde
parent 30f359ee3c
commit 7f91cbc397
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 16 additions and 0 deletions

View file

@ -245,6 +245,7 @@ struct _VariantCall {
VCALL_LOCALMEM3R(String, count);
VCALL_LOCALMEM3R(String, countn);
VCALL_LOCALMEM2R(String, substr);
VCALL_LOCALMEM2R(String, get_slice);
VCALL_LOCALMEM2R(String, find);
VCALL_LOCALMEM1R(String, find_last);
VCALL_LOCALMEM2R(String, findn);
@ -1633,6 +1634,7 @@ void register_variant_methods() {
ADDFUNC1R(STRING, INT, String, naturalnocasecmp_to, STRING, "to", varray());
ADDFUNC0R(STRING, INT, String, length, varray());
ADDFUNC2R(STRING, STRING, String, substr, INT, "from", INT, "len", varray(-1));
ADDFUNC2R(STRING, STRING, String, get_slice, STRING, "delimiter", INT, "slice", varray());
ADDFUNC2R(STRING, INT, String, find, STRING, "what", INT, "from", varray(0));

View file

@ -335,6 +335,19 @@
If the string is a valid file path, returns the filename.
</description>
</method>
<method name="get_slice">
<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">
<return type="int" />
<description>
@ -690,6 +703,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:
[codeblock]
var some_string = "One,Two,Three,Four"