- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: Seeking Elegance
Tue, 2012-01-24, 20:47
s.head.toUpper + s.tail.toLower?
Am 24.01.2012 20:42, schrieb Erik Osheim:
> On Tue, Jan 24, 2012 at 11:30:53AM -0800, Eric Kolotyluk wrote:
>> I have
>>
>> val commandArray = command.toLowerCase().toCharArray()
>> commandArray(0) = commandArray(0).toUpperCase
>> val className = commandArray.toString()
>>
>> as a way to convert a string to lower case and capitalize the first
>> character, but I suspect there may be a more elegant way to do this
>> in Scala. Can anyone think of a more elegant way that is clear?
> I would have thought of something more like:
>
> def titleCase(s:String) = s(0).toUpper + s.substring(1).toLower
>
> Elegance is in the eye of the beholder, but this method involves less
> work and is shorter.
>
On Tue, Jan 24, 2012 at 02:46:03PM -0500, Doug Tangren wrote:
> "bIpPy".toLowerCase.capitalize
Oooh, didn't know about capitalize! Thanks :)