Skip to content

Commit

Permalink
Add Char * String and Char * Char (JuliaLang#22532)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Adams authored and jeffwong committed Jul 24, 2017
1 parent 1e030ab commit 8fcb668
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
15 changes: 8 additions & 7 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,21 @@ sizeof(s::AbstractString) = error("type $(typeof(s)) has no canonical binary rep
eltype(::Type{<:AbstractString}) = Char

"""
```
*(s::AbstractString, t::AbstractString)
```
*(s::Union{AbstractString, Char}, t::Union{AbstractString, Char}...)
Concatenate strings. The `*` operator is an alias to this function.
# Example
Concatenate strings and/or characters, producing a [`String`](@ref). This is equivalent
to calling the [`string`](@ref) function on the arguments.
# Examples
```jldoctest
julia> "Hello " * "world"
"Hello world"
julia> 'j' * "ulia"
"julia"
```
"""
(*)(s1::AbstractString, ss::AbstractString...) = string(s1, ss...)
(*)(s1::Union{Char, AbstractString}, ss::Union{Char, AbstractString}...) = string(s1, ss...)

one(::Union{T,Type{T}}) where {T<:AbstractString} = convert(T, "")

Expand Down
7 changes: 7 additions & 0 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,10 @@ Base.endof(x::CharStr) = endof(x.chars)
# issue #12495: check that logical indexing attempt raises ArgumentError
@test_throws ArgumentError "abc"[[true, false, true]]
@test_throws ArgumentError "abc"[BitArray([true, false, true])]

@test "ab" * "cd" == "abcd"
@test 'a' * "bc" == "abc"
@test "ab" * 'c' == "abc"
@test 'a' * 'b' == "ab"
@test 'a' * "b" * 'c' == "abc"
@test "a" * 'b' * 'c' == "abc"

0 comments on commit 8fcb668

Please sign in to comment.