Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala how can I uppercase first character and lowercase others

Tags:

scala

I basically have a form that takes user input and I match it in the database. I want the first character uppercase and characters with spaces and all others lowercase. Right now I have this

  location= location.split(' ').map(_.capitalize).mkString(" ")

The code above does this: if a user types 'new york' it gets converted to 'New York', however if a user types NeW YoRk I still want it converted to the form 'New York'.

like image 461
user1949387 Avatar asked Feb 06 '16 18:02

user1949387


People also ask

How do you change the first character of a string to uppercase?

To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it. Now, we would get the remaining characters of the string using the slice() function.

How do you capitalize in Scala?

The way this works is that you split the given String into “words” with split , which creates an Array[String] ; then capitalize each word in the array with map ; then convert the array back to a String .

How do you write the first letter of a string to lowercase?

What is the most efficient way to make the first character of a String lower case? String input = "SomeInputString"; char c[] = input. toCharArray(); c[0] = Character. toLowerCase(c[0]); String output = new String(c);

How can we convert a string to uppercase and lowercase?

The toUpperCase() method converts a string to upper case letters. Note: The toLowerCase() method converts a string to lower case letters.


1 Answers

Just convert it all to lowercase first:

_.toLowerCase.capitalize
like image 173
dth Avatar answered Oct 25 '22 02:10

dth