Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalise first letter in String [duplicate]

I'm having trouble converting the first letter to Capital in a String:

rackingSystem.toLowerCase(); // has capitals in every word, so first convert all to lower case StringBuilder rackingSystemSb = new StringBuilder(); rackingSystemSb.append(rackingSystem); rackingSystemSb.setCharAt(0, Character.toUpperCase(rackingSystemSb.charAt(0)));  rackingSystem = rackingSystemSb.toString(); 

This doesn't seem to work..

Any suggestions?

like image 201
Scamparelli Avatar asked Mar 06 '13 22:03

Scamparelli


People also ask

How do you capitalize first letter in text?

To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.

Which function returns a copy of the string with its first character capitalized?

Python String capitalize() method returns a copy of the string with only its first character capitalized.

How do you replace the first character in a string with uppercase?

You can use str. title() to get a title cased version of the string. This converts the first character of each word in the string to uppercase and the remaining characters to lowercase.


1 Answers

Try doing:

rackingSystem = rackingSystem.toLowerCase(); 

Instead of:

rackingSystem.toLowerCase();  

Strings are immutable, you must reassign the result of toLowerCase().

Easier though, (as long as your String is larger than length 2):

rackingSystem = rackingSystem.substring(0,1).toUpperCase() + rackingSystem.substring(1).toLowerCase(); 
like image 100
A--C Avatar answered Sep 23 '22 14:09

A--C