diff --git a/AK/AKString.h b/AK/AKString.h index 617bb6ab392..8f1119d80e7 100644 --- a/AK/AKString.h +++ b/AK/AKString.h @@ -109,6 +109,7 @@ public: return m_impl->to_uppercase(); } + Vector split_limit(char separator, int limit) const; Vector split(char separator) const; String substring(int start, int length) const; diff --git a/AK/String.cpp b/AK/String.cpp index 8bfc7876865..a7ac12fded2 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -68,13 +68,18 @@ StringView String::substring_view(int start, int length) const } Vector String::split(const char separator) const +{ + return split_limit(separator, 0); +} + +Vector String::split_limit(const char separator, int limit) const { if (is_empty()) return {}; Vector v; ssize_t substart = 0; - for (ssize_t i = 0; i < length(); ++i) { + for (ssize_t i = 0; i < length() && (v.size() + 1) != limit; ++i) { char ch = characters()[i]; if (ch == separator) { ssize_t sublen = i - substart;