equivalent of python's string.title() in julia

191 views
Skip to first unread message

julialan...@gmail.com

unread,
Feb 4, 2016, 11:39:16 PM2/4/16
to julia-users
in python, "abc def ghi jkl".title() capitalizes each "word" in the string, giving 'Abc Def Ghi Jkl' as the result

i have not been able to find a similar string function in julia, so i'm currently doing this:

julia> join(map(ucfirst, split("abc def ghi jkl")), " ")
"Abc Def Ghi Jkl"

is there a better / more straightforward julia-based solution?

thanks for any suggestion!

Milan Bouchet-Valat

unread,
Feb 5, 2016, 4:06:40 AM2/5/16
to julia...@googlegroups.com
Le jeudi 04 février 2016 à 17:31 -0800, julialan...@gmail.com a
écrit :
I wouldn't call it "straightforward", but if you need a Unicode-correct
way of applying title case, you can use UnicodeExtras, which relies on
ICU:
https://github.com/nolta/UnicodeExtras.jl#case-handling

Title case hasn't been implemented in base yet, though it's been
mentioned in the past:
https://github.com/JuliaLang/julia/issues/11471
https://github.com/JuliaLang/julia/issues/774


Regards

Stefan Karpinski

unread,
Feb 5, 2016, 9:52:16 AM2/5/16
to Julia Users
Since there was talk of removing uppercase and lowercase altogether (I was against this), I felt it would be pushing it to add titlecase, but a good PR to do this would be welcomed.

Ismael Venegas Castelló

unread,
Feb 5, 2016, 10:06:23 AM2/5/16
to julia-users
You can do something like:

title(s::AbstractString) = join([ucfirst(w) for w in split(s, ' ')], ' ')::typeof(s)

Ismael Venegas Castelló

unread,
Feb 5, 2016, 10:37:25 AM2/5/16
to julia-users
I made this IJulia gist:

bit.ly/JLTitle

Steven G. Johnson

unread,
Feb 5, 2016, 1:12:44 PM2/5/16
to julia-users


On Friday, February 5, 2016 at 4:06:40 AM UTC-5, Milan Bouchet-Valat wrote:
> is there a better / more straightforward julia-based solution?
I wouldn't call it "straightforward", but if you need a Unicode-correct
way of applying title case, you can use UnicodeExtras, which relies on
ICU:
https://github.com/nolta/UnicodeExtras.jl#case-handling

utf8proc (which is used internally for Unicode transformations by Julia) has Unicode-aware titlecase mapping, but we don't currently export that; would require an easy PR similar to https://github.com/JuliaLang/utf8proc/pull/40 
Reply all
Reply to author
Forward
0 new messages