Split keepempty

What does keepempty mean in split? Is there a good example?

• keepempty: whether empty fields should be kept in the result. Default is false without a dlm argument, true with a dlm argument.

julia> split("hello;there;user;;;foo", ";"; limit=4, keepempty=false)
4-element Vector{SubString{String}}:
 "hello"
 "there"
 "user"
 ";;foo"

In your example it’s not obvious what the difference is between keepempty=false and true because the empty fields occur after the 4th split and you have limit=4. Removing this we can see:

julia> split("hello;there;user;;;foo", ";"; keepempty=true)
6-element Vector{SubString{String}}:
 "hello"
 "there"
 "user"
 ""
 ""
 "foo"

julia> split("hello;there;user;;;foo", ";"; keepempty=false)
4-element Vector{SubString{String}}:
 "hello"
 "there"
 "user"
 "foo"

Essentially, if you have two delimiters in a row (ie “;;”) it controls whether to return an empty string or to ignore it.

HTH

1 Like