diff --git a/base/strings/util.jl b/base/strings/util.jl index 73ab4b0f2867a..d6d65ebd7dd99 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -96,6 +96,9 @@ julia> chop(a, head = 5, tail = 5) ``` """ function chop(s::AbstractString; head::Integer = 0, tail::Integer = 1) + if isempty(s) + return SubString(s) + end SubString(s, nextind(s, firstindex(s), head), prevind(s, lastindex(s), tail)) end diff --git a/test/strings/util.jl b/test/strings/util.jl index b675bdf6531b8..43d88853c4db9 100644 --- a/test/strings/util.jl +++ b/test/strings/util.jl @@ -302,6 +302,7 @@ end @test chomp("foo\r\n") == "foo" @test chomp("fo∀\r\n") == "fo∀" @test chomp("fo∀") == "fo∀" + @test chop("") == "" @test chop("fooε") == "foo" @test chop("foεo") == "foε" @test chop("∃∃∃∃") == "∃∃∃"