Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


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;
share|improve this question

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()) + ' ';
share|improve this answer
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 '14 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); – Simon Lawrence Feb 18 '14 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 '14 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. – Simon Lawrence Feb 18 '14 at 10:27
You can use the String method .capitalize() in your for loop – Scott Pelak Oct 30 '15 at 15:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.