Skip to content
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

use titlecase, not uppercase, for ucfirst #21967

Merged
merged 1 commit into from
May 21, 2017
Merged
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
12 changes: 10 additions & 2 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -418,15 +420,21 @@ 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")
"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

"""
Expand Down
1 change: 1 addition & 0 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down