diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 409aeddead..16f54c2088 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -618,6 +618,36 @@ fn (s string) + (a string) string { return res } +// split_any splits the string to an array by any of the `delim` chars. +// Example: "first row\nsecond row".split_any(" \n") == ['first', 'row', 'second', 'row'] +// Split a string using the chars in the delimiter string as delimiters chars. +// If the delimiter string is empty then `.split()` is used. +[direct_array_access] +pub fn (s string) split_any(delim string) []string { + mut res := []string{} + mut i := 0 + // check empty source string + if s.len > 0 { + // if empty delimiter string using defautl split + if delim.len <= 0 { + return s.split('') + } + for index, ch in s { + for delim_ch in delim { + if ch == delim_ch { + res << s[i..index] + i = index + 1 + break + } + } + } + if i < s.len { + res << s[i..] + } + } + return res +} + // split splits the string to an array by `delim`. // Example: assert 'A B C'.split(' ') == ['A','B','C'] // If `delim` is empty the string is split by it's characters. diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index 9e0b23b503..f6a975dfb2 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -229,6 +229,17 @@ fn test_split() { assert vals[1] == '' } +fn test_split_any() { + assert 'ABC'.split_any('') == ['A', 'B', 'C'] + assert ''.split_any(' ') == [] + assert ' '.split_any(' ') == [''] + assert ' '.split_any(' ') == ['', ''] + assert 'Ciao come stai? '.split_any(' ') == ['Ciao', 'come', 'stai?'] + assert 'Ciao+come*stai? '.split_any('+*') == ['Ciao', 'come', 'stai? '] + assert 'Ciao+come*stai? '.split_any('+* ') == ['Ciao', 'come', 'stai?'] + assert 'first row\nsecond row'.split_any(' \n') == ['first', 'row', 'second', 'row'] +} + fn test_trim_space() { a := ' a ' assert a.trim_space() == 'a'