string: update split() method

pull/2723/head
Don Alfons Nisnoni 2019-11-11 00:37:36 +08:00 committed by Alexander Medvednikov
parent d1e7a54f3a
commit b9728c7af0
2 changed files with 18 additions and 1 deletions

View File

@ -310,8 +310,14 @@ fn (s string) add(a string) string {
pub fn (s string) split(delim string) []string {
// println('string split delim="$delim" s="$s"')
mut res := []string
// if delim.len == 0 {
// res << s
// return res
// }
if delim.len == 0 {
res << s
for ch in s {
res << ch.str()
}
return res
}
if delim.len == 1 {

View File

@ -113,6 +113,17 @@ fn test_split() {
assert vals[0] == 'l'
assert vals[1] == 'l'
assert vals[2] == 'l'
// /////////
s = 'awesome'
a := s.split('')
assert a.len == 7
assert a[0] == 'a'
assert a[1] == 'w'
assert a[2] == 'e'
assert a[3] == 's'
assert a[4] == 'o'
assert a[5] == 'm'
assert a[6] == 'e'
}
fn test_trim_space() {