diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index b34d9f32711..1b3b070592b 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -1157,6 +1157,14 @@ Vector String::split_spaces() const { Vector String::split(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const { Vector ret; + + if (is_empty()) { + if (p_allow_empty) { + ret.push_back(""); + } + return ret; + } + int from = 0; int len = length(); diff --git a/tests/core/string/test_string.h b/tests/core/string/test_string.h index 0cf3448a482..5d19b5a1649 100644 --- a/tests/core/string/test_string.h +++ b/tests/core/string/test_string.h @@ -512,6 +512,14 @@ TEST_CASE("[String] Splitting") { CHECK(l[i] == slices_3[i]); } + s = ""; + l = s.split(); + CHECK(l.size() == 1); + CHECK(l[0] == ""); + + l = s.split("", false); + CHECK(l.size() == 0); + s = "Mars Jupiter Saturn Uranus"; const char *slices_s[4] = { "Mars", "Jupiter", "Saturn", "Uranus" }; l = s.split_spaces();