string_trim_end

This function returns a new string with all trailing white-space characters removed (i.e. on the right side of the string).

Passing an array of strings as the second argument (substr) will make the function remove any of those substrings at the end of the string, instead of white-space. These substrings may be repeated at the end, in which case all continuous occurrences will be removed. See Example 3 below.

NOTE The following characters are white-space characters: space (" "), tab ("\t"), carriage return ("\r"), newline ("\n"), form feed ("\f") and vertical tab ("\v"). See White-space Characters for the full list, including Unicode characters.

 

Syntax:

string_trim_end(str, [substr]);

ArgumentTypeDescription
strStringThe string to trim
substrArray of StringsOPTIONAL An array containing strings to trim from the end of the string

 

Returns:

String

 

Example 1:

var _the_string = "A\nB\n\C\nD\n\n\n\n\n\n";
var _clean_string = string_trim_end(_the_string);

The above code first defines a string with many newlines at the end and stores it in a temporary variable _the_string. It then calls string_trim_end to remove all the newline characters at the end of the string (but not the ones between the letters) and stores the result in another temporary variable _clean_string.

Example 2 (Using An Array):

var _string = "This is an object I lovelove"
var _remove = ["This", "I", "love"]
var _trimmed = string_trim_end(_string, _remove);

show_debug_message(_trimmed) // Prints "This is an object I "

This removes the word "love" from the end of the string. "love" appears twice at the end and is removed both times. The word "I" is not removed, because it's not at the very end of the string, and "This" is not removed as it's at the start.