diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp
index a30a5deb932..92c9e4b70cd 100644
--- a/core/string/ustring.cpp
+++ b/core/string/ustring.cpp
@@ -2857,6 +2857,12 @@ String String::insert(int p_at_pos, const String &p_string) const {
return pre + p_string + post;
}
+String String::erase(int p_pos, int p_chars) const {
+ ERR_FAIL_COND_V_MSG(p_pos < 0, "", vformat("Invalid starting position for `String.erase()`: %d. Starting position must be positive or zero.", p_pos));
+ ERR_FAIL_COND_V_MSG(p_chars < 0, "", vformat("Invalid character count for `String.erase()`: %d. Character count must be positive or zero.", p_chars));
+ return left(p_pos) + substr(p_pos + p_chars);
+}
+
String String::substr(int p_from, int p_chars) const {
if (p_chars == -1) {
p_chars = length() - p_from;
diff --git a/core/string/ustring.h b/core/string/ustring.h
index e1512cfb261..c771dff5156 100644
--- a/core/string/ustring.h
+++ b/core/string/ustring.h
@@ -304,6 +304,7 @@ public:
String replacen(const String &p_key, const String &p_with) const;
String repeat(int p_count) const;
String insert(int p_at_pos, const String &p_string) const;
+ String erase(int p_pos, int p_chars = 1) const;
String pad_decimals(int p_digits) const;
String pad_zeros(int p_digits) const;
String trim_prefix(const String &p_prefix) const;
diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp
index e83b6dc1838..d77222b1669 100644
--- a/core/variant/variant_call.cpp
+++ b/core/variant/variant_call.cpp
@@ -1659,6 +1659,7 @@ static void _register_variant_builtin_methods() {
bind_string_method(replacen, sarray("what", "forwhat"), varray());
bind_string_method(repeat, sarray("count"), varray());
bind_string_method(insert, sarray("position", "what"), varray());
+ bind_string_method(erase, sarray("position", "chars"), varray(1));
bind_string_method(capitalize, sarray(), varray());
bind_string_method(to_camel_case, sarray(), varray());
bind_string_method(to_pascal_case, sarray(), varray());
diff --git a/doc/classes/String.xml b/doc/classes/String.xml
index fd50b308c3d..d771566688d 100644
--- a/doc/classes/String.xml
+++ b/doc/classes/String.xml
@@ -175,6 +175,14 @@
Returns [code]true[/code] if the string ends with the given [param text]. See also [method begins_with].
+
+
+
+
+
+ Returns a string with [param chars] characters erased starting from [param position]. If [param chars] goes beyond the string's length given the specified [param position], fewer characters will be erased from the returned string. Returns an empty string if either [code]position[/code] or [code]chars[/code] is negative. Returns the original string unmodified if [param chars] is [code]0[/code].
+
+
diff --git a/doc/classes/StringName.xml b/doc/classes/StringName.xml
index 5b630a092ee..a74b78f7e31 100644
--- a/doc/classes/StringName.xml
+++ b/doc/classes/StringName.xml
@@ -158,6 +158,14 @@
Returns [code]true[/code] if the string ends with the given [param text]. See also [method begins_with].
+
+
+
+
+
+ Returns a string with [param chars] characters erased starting from [param position]. If [param chars] goes beyond the string's length given the specified [param position], fewer characters will be erased from the returned string. Returns an empty string if either [code]position[/code] or [code]chars[/code] is negative. Returns the original string unmodified if [param chars] is [code]0[/code].
+
+
diff --git a/tests/core/string/test_string.h b/tests/core/string/test_string.h
index abe9f78ccc9..7c76e7aa7bc 100644
--- a/tests/core/string/test_string.h
+++ b/tests/core/string/test_string.h
@@ -395,6 +395,12 @@ TEST_CASE("[String] Insertion") {
CHECK(s == "Who is Frederic Chopin?");
}
+TEST_CASE("[String] Erasing") {
+ String s = "Josephine is such a cute girl!";
+ s = s.erase(s.find("cute "), String("cute ").length());
+ CHECK(s == "Josephine is such a girl!");
+}
+
TEST_CASE("[String] Number to string") {
CHECK(String::num(0) == "0");
CHECK(String::num(0.0) == "0"); // No trailing zeros.