diff --git a/base/strings/basic.jl b/base/strings/basic.jl index df3e4bdf6db81..e31b2218de920 100644 --- a/base/strings/basic.jl +++ b/base/strings/basic.jl @@ -394,6 +394,8 @@ lowercase(s::AbstractString) = map(lowercase, s) titlecase(s::AbstractString) Capitalizes the first character of each word in `s`. +See also [`ucfirst`](@ref) to capitalize only the first +character in `s`. ```jldoctest julia> titlecase("the julia programming language") @@ -418,7 +420,10 @@ end """ ucfirst(s::AbstractString) -Returns `string` with the first character converted to uppercase. +Returns `string` with the first character converted to uppercase +(technically "title case" for Unicode). +See also [`titlecase`](@ref) to capitalize the first character of +every word in `s`. ```jldoctest julia> ucfirst("python") @@ -426,7 +431,10 @@ julia> ucfirst("python") ``` """ function ucfirst(s::AbstractString) - isempty(s) || isupper(s[1]) ? s : string(uppercase(s[1]),s[nextind(s,1):end]) + isempty(s) && return s + c = s[1] + tc = titlecase(c) + return c==tc ? s : string(tc,s[nextind(s,1):end]) end """ diff --git a/test/strings/basic.jl b/test/strings/basic.jl index b45b6757d80af..4c155f92b1b27 100644 --- a/test/strings/basic.jl +++ b/test/strings/basic.jl @@ -151,6 +151,7 @@ end @test ucfirst("hola")=="Hola" @test ucfirst("")=="" @test ucfirst("*")=="*" +@test ucfirst("DŽxx") == ucfirst("džxx") == "Džxx" @test lcfirst("Hola")=="hola" @test lcfirst("hola")=="hola"