Skip to content

Add splitlines #20390

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ export
showcompact,
showerror,
split,
splitlines,
sprint,
string,
strip,
Expand Down
1 change: 1 addition & 0 deletions base/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ precompile(Base.splice!, (Array{UInt8, 1}, Base.UnitRange{Int}, Array{UInt8, 1})
precompile(Base.split, (String, String))
precompile(Base.split, (String, Regex))
precompile(Base.split, (String,))
precompile(Base.splitlines, (String,))
precompile(Base.srand, (Array{UInt32,1},))
precompile(Base.start, (Array{Base.LineEdit.TextInterface, 1},))
precompile(Base.start, (Dict{Any,Any},))
Expand Down
11 changes: 11 additions & 0 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,17 @@ end
# a bit oddball, but standard behavior in Perl, Ruby & Python:
split(str::AbstractString) = split(str, _default_delims; limit=0, keep=false)

"""
splitlines(str::AbstractString; chomp = true)

Return an array of strings by splitting the given string at line break characters
(`'\\n'` or `"\\r\\n"`). When `chomp` is true (as it is by default),
these trailing newline characters are removed from the
line before it is returned. When `chomp` is false, they are returned as part of the
line.
"""
splitlines(str::AbstractString; chomp = true) = readlines(IOBuffer(str), chomp = chomp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a doctest here showing how it is used?


rsplit{T<:SubString}(str::T, splitter; limit::Integer=0, keep::Bool=true) = _rsplit(str, splitter, limit, keep, T[])

"""
Expand Down
1 change: 1 addition & 0 deletions doc/src/stdlib/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Base.contains
Base.reverse(::AbstractString)
Base.replace
Base.split
Base.splitlines
Base.rsplit
Base.strip
Base.lstrip
Expand Down
4 changes: 4 additions & 0 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ end
@test isequal(split("abcd", r"d+"), ["abc",""])
@test isequal(split("abcd", r"[ad]?"), ["","b","c",""])

@test isequal(splitlines("α\nβ\nγ\nδ"), String["α", "β", "γ", "δ"])
@test isequal(splitlines("α\r\nβ\r\nγ\r\nδ"), String["α", "β", "γ", "δ"])
@test isequal(splitlines("α\nβ\nγ\nδ", chomp=false), String["α\n","β\n","γ\n","δ"])

# replace
@test replace("\u2202", '*', '\0') == "\u2202"

Expand Down