diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs index 4580d02b9b5..14accff2a88 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs @@ -330,6 +330,15 @@ namespace Godot return instance.IndexOf(what, from, caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase); } + /// Find the first occurrence of a char. Optionally, the search starting position can be passed. + /// The first instance of the char, or -1 if not found. + public static int Find(this string instance, char what, int from = 0, bool caseSensitive = true) + { + // TODO: Could be more efficient if we get a char version of `IndexOf`. + // See https://github.com/dotnet/runtime/issues/44116 + return instance.IndexOf(what.ToString(), from, caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase); + } + /// Find the last occurrence of a substring. /// The starting position of the substring, or -1 if not found. public static int FindLast(this string instance, string what, bool caseSensitive = true) @@ -666,6 +675,33 @@ namespace Godot return instance.Length; } + /// + /// Returns a copy of the string with characters removed from the left. + /// + /// The string to remove characters from. + /// The characters to be removed. + /// A copy of the string with characters removed from the left. + public static string LStrip(this string instance, string chars) + { + int len = instance.Length; + int beg; + + for (beg = 0; beg < len; beg++) + { + if (chars.Find(instance[beg]) == -1) + { + break; + } + } + + if (beg == 0) + { + return instance; + } + + return instance.Substr(beg, len - beg); + } + /// /// Do a simple expression match, where '*' matches zero or more arbitrary characters and '?' matches any single character except '.'. /// @@ -894,6 +930,33 @@ namespace Godot return instance.Substring(pos, instance.Length - pos); } + /// + /// Returns a copy of the string with characters removed from the right. + /// + /// The string to remove characters from. + /// The characters to be removed. + /// A copy of the string with characters removed from the right. + public static string RStrip(this string instance, string chars) + { + int len = instance.Length; + int end; + + for (end = len - 1; end >= 0; end--) + { + if (chars.Find(instance[end]) == -1) + { + break; + } + } + + if (end == len - 1) + { + return instance; + } + + return instance.Substr(0, end + 1); + } + public static byte[] SHA256Buffer(this string instance) { return godot_icall_String_sha256_buffer(instance);