Apex has a String method to change all characters to lower case String.toLowerCase and a method to change all characters to upper case String.toUpperCase, but it doesn’t have a method to properly capitalize just the first letter of a name. This can be extremely frustrating when getting data from 3rd party sources that don’t handle it for you. Advanced coders can solve this with a regular expression but for the rest of us, we can use our existing tools like so:
public static String proper(String s)
{
String result = '';
if (s != null)
{
List<String> words = s.split(' ');
for (String w : words)
{
String letter1 = w.substring(0,1).toUpperCase();
result += letter1;
if (w.length() > 1)
{
String letterx = w.substring(1,w.length()).toLowerCase();
result += letterx;
}
result += ' ';
}
result.substring(0,result.length()-1);
}
return result;
}