5

In sObject, I have a field called Rep_Name which contains First name and Last name of the rep, separated by a space. However, the name is not formatted.. I want to convert 1st letter of each word to capital and rest to small. Is there a way to achieve this?

controller:

private string nameFormat(String repname1) {
List<String> elems = repname1.split(' ');
repname1= '';
for (String x : elems)
{
    repname1 += x.substring(0,1).toUpperCase()+x.substring(1,x.length()) + ' ';
}
return repname1;
}

public  String repname {
        get {
       Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;               

if( OSR != null){
    List<User> Results = [Select Name
                          From User 
                          Where Sales_Rep__c =: OSR limit 1];

    return nameFormat(Results.get(0).Name);
    }
    else {
    return null;
    }
  }
 set;
    } 

4 Answers 4

11

I don't think Salesforce provides a method for that. You probably need a trigger before insert/update, the following code works for me:

String rep_name = 'david john mitch doe';
List<String> elems = rep_name.split(' ');
rep_name = '';
for (String x : elems)
{
    rep_name += x.substring(0,1).toUpperCase()+x.substring(1,x.length()) + ' ';
}
System.debug('>>>'+rep_name);
5
  • Hi brovasi. Thanks for the reply. I tried adding your snippet of the code. There was no compilation error but the fetching data gave an null pointer error. I have updated my question with the controller code. Can you help me with that? Thanks..
    – Robin
    Feb 18, 2014 at 8:35
  • you need to put lines 2 - 7 in their own method in your controller defined as private string nameFormat(String rep_name) {} then in your controller you need to return: return nameFormat(Results.get(0).Name); Feb 18, 2014 at 9:38
  • @Simon I have edited the controller code (see above). It doesnt throw an error but returns still returns the unformatted name. I am sure there is something very small that I am missing. Please help. Thanks
    – Robin
    Feb 18, 2014 at 10:20
  • It works for me in a demo page and controller? What is OSR though, and regnInt, I assume these are handled else where in your controller? The capitalisation function definitely works, I am using it to load a User's name and capitalise the first two characters (by tweeking it) and it works great. Feb 18, 2014 at 10:27
  • 1
    You can use the String method .capitalize() in your for loop Oct 30, 2015 at 15:48
8

Not sure when this method was added, but there is now a String method called capitalize, which greatly simplifies things:

String name = 'alice bob charles';
List<String> names = name.split(' ');
for (Integer i = 0; i < names.size(); i++)
    names[i] = names[i].capitalize();
name = String.join(names, ' ');
system.debug(name); // yields Alice Bob Charles
1
  • 1
    A few releases ago :D
    – sfdcfox
    May 26, 2017 at 14:49
1

This is an old post, but wanted to share some changes I made for anyone else who may come across this page. I was getting OOB errors for names like John A Smith and John A. Smith so I created the following, based on @Cloud Ninja's answer:

private static String nameFix(String name){
    name = name.toLowerCase();
    name = name.remove('.');
    name = name.remove(',');
    System.debug('Name = '+ name);
    List<String> nameGroup = name.split(' ');
    String new_name = '';
    for (String part : nameGroup){
        if(part.length() == 1 ){
            part = part.toUpperCase()+ ' ';
            new_name += part;
        }
        else if (part.length() >= 2){
            System.debug(part);
            new_name += part.subString(0,1).toUpperCase()+part.subString(1,part.length())+ ' ';
        }
    }
    return new_name; 

If you aren't worried about .'s or ,'s, you can just include the If/Else block...

1
  • I hadn't seen this post, but there is a simpler way. You prompted me to add an answer as well.
    – Adrian Larson
    May 26, 2017 at 14:48
0

Pretty much the same thing as answered by Adrian but with for each loop:

String name = 'alice bob charles';
List<String> normalizedStringList = new List<String>();
for(String eachWord : name.split(' ')){
    normalizedStringList.add(eachWord.capitalize());
}
name = String.join(normalizedStringList, ' ');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .