diff --git a/base/exports.jl b/base/exports.jl index 829885b21da87..58044856d3e2a 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -845,6 +845,7 @@ export showcompact, showerror, split, + splitlines, sprint, string, strip, diff --git a/base/precompile.jl b/base/precompile.jl index 60f0878609a39..9ad8493e3e512 100644 --- a/base/precompile.jl +++ b/base/precompile.jl @@ -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},)) diff --git a/base/strings/util.jl b/base/strings/util.jl index ddc1c92fe3572..5dadccedcc29b 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -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) + rsplit{T<:SubString}(str::T, splitter; limit::Integer=0, keep::Bool=true) = _rsplit(str, splitter, limit, keep, T[]) """ diff --git a/doc/src/stdlib/strings.md b/doc/src/stdlib/strings.md index be655da87e526..fc79c32a18d5a 100644 --- a/doc/src/stdlib/strings.md +++ b/doc/src/stdlib/strings.md @@ -35,6 +35,7 @@ Base.contains Base.reverse(::AbstractString) Base.replace Base.split +Base.splitlines Base.rsplit Base.strip Base.lstrip diff --git a/test/strings/util.jl b/test/strings/util.jl index 21072a524bf4c..1a55946b6357e 100644 --- a/test/strings/util.jl +++ b/test/strings/util.jl @@ -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"