This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: Seeking Elegance

1 reply
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.

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.
>

d_m
Joined: 2010-11-11,
User offline. Last seen 35 weeks 2 days ago.
Re: Seeking Elegance

On Tue, Jan 24, 2012 at 02:46:03PM -0500, Doug Tangren wrote:
> "bIpPy".toLowerCase.capitalize

Oooh, didn't know about capitalize! Thanks :)

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland