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'.
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.
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 .
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);
The toUpperCase() method converts a string to upper case letters. Note: The toLowerCase() method converts a string to lower case letters.
Just convert it all to lowercase first:
_.toLowerCase.capitalize
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With