Add String.splitLimit API (#666)

* Add String.splitLimit API

* Update stdlib/base.pkl

Co-authored-by: Daniel Chao <daniel.h.chao@gmail.com>

---------

Co-authored-by: Daniel Chao <daniel.h.chao@gmail.com>
This commit is contained in:
Josh B
2024-10-07 07:28:19 -07:00
committed by GitHub
parent 9e7e42eb53
commit 1e63c48ce4
5 changed files with 133 additions and 48 deletions

View File

@@ -1367,6 +1367,21 @@ external class String extends Any {
/// Splits this string around matches of [pattern].
external function split(pattern: String|Regex): List<String>
/// Splits this string matches of [pattern], up to [limit] substrings.
///
/// Returns a [List] with at most [limit] elements.
/// If the limit has been reached, the last entry will contain the un-split remainder of this string.
///
/// Facts:
/// ```
/// "a.b.c".splitLimit(".", 2) == List("a", "b.c")
/// "a.b.c".splitLimit(".", 1) == List("a.b.c")
/// "a.b.c".splitLimit(".", 50) == List("a", "b", "c")
/// "a.b:c".splitLimit(Regex("[.:]"), 3) == List("a", "b", "c")
/// ```
@Since { version = "0.27.0" }
external function splitLimit(pattern: String|Regex, limit: Int(this > 0)): List<String>
/// Converts the first character of this string to title case.
///
/// Facts: