Skip to content

Commit 31a3017

Browse files
committed
feat: add split function to string utils
Different from native squirrel split which considers every character in the `_delimiter` string to be a delimiter rather than the entire `_delimiter` string as the delimiter.
1 parent d22b242 commit 31a3017

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

msu/utils/string.nut

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,32 @@
3434
{
3535
return _end.len() <= _string.len() && _string.slice(-_end.len()) == _end;
3636
}
37+
38+
function split( _string, _delimiter, _skipEmpty = true )
39+
{
40+
if (_string == "")
41+
return [];
42+
43+
if (_delimiter.len() == 1 && _skipEmpty)
44+
return ::split(_string, _delimiter);
45+
46+
local ret = [];
47+
local idx;
48+
while ((idx = _string.find(_delimiter)) != null)
49+
{
50+
local val = _string.slice(0, idx);
51+
if (!_skipEmpty || val != "")
52+
{
53+
ret.push(val);
54+
}
55+
_string = _string.slice(idx + _delimiter.len());
56+
}
57+
58+
if (!_skipEmpty || _string != "")
59+
{
60+
ret.push(_string);
61+
}
62+
63+
return ret;
64+
}
3765
}

0 commit comments

Comments
 (0)