Use a substring to split a string into an array of items

string:split_by_substring(STRING $string, STRING $substring, INT $limit=0) : ARRAY

string:split_by_substring('hello world',' ') 
#=> [ "hello", "world" ]

string:split_by_substring('hello world','o') 
#=> [
#=>     "hell",
#=>     " w",
#=>     "rld"
#=> ]

string:split_by_substring('hello world','o',2) 
#=> [
#=>     "hell",
#=>     " world"
#=> ]

# note: $substring cannot be empty!
string:split_by_substring('hello world','') 
#=> Error: cannot use an empty substring as a delimiter to split the given type_string.

# note: $string can be empty
string:split_by_substring('','something')
#=> [ "" ]

Copyright ©2013-2022 SunSed®. All rights reserved.