Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add usage details for Base.eachsplit #46751

Closed
logankilpatrick opened this issue Sep 14, 2022 · 1 comment
Closed

Add usage details for Base.eachsplit #46751

logankilpatrick opened this issue Sep 14, 2022 · 1 comment
Labels
docs This change adds or pertains to documentation good first issue Indicates a good issue for first-time contributors to Julia strings "Strings!"

Comments

@logankilpatrick
Copy link
Member

I am reading through the docs for [Base.eachsplit](https://docs.julialang.org/en/v1/base/strings/#Base.eachsplit) and right now its use case is not clear to me. I see that the type it returns is different than split, but the current example does not make it clear why I would want to use it instead of just split:

julia> split(a, ".")
2-element Vector{SubString{String}}:
 "Ma"
 "rch"

julia> collect(eachsplit(a, "."))
2-element Vector{SubString}:
 "Ma"
 "rch"
@fredrikekre fredrikekre added docs This change adds or pertains to documentation good first issue Indicates a good issue for first-time contributors to Julia strings "Strings!" labels Sep 14, 2022
@jakobjpeters
Copy link

It looks like split primarily calls collect on eachsplit.

julia/base/strings/util.jl

Lines 592 to 596 in afb6c60

function split(str::T, splitter;
limit::Integer=0, keepempty::Bool=true) where {T<:AbstractString}
itr = eachsplit(str, splitter; limit, keepempty)
collect(T <: SubString ? T : SubString{T}, itr)
end

The example for eachsplit could be made to match that for eachmatch.

julia> a = "Ma.rch"
"Ma.rch"

julia> b = eachsplit(a, ".")
Base.SplitIterator{String, String}("Ma.rch", ".", 0, true)

julia> collect(b)
2-element Vector{SubString}:
 "Ma"
 "rch"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation good first issue Indicates a good issue for first-time contributors to Julia strings "Strings!"
Projects
None yet
Development

No branches or pull requests

3 participants