1

I have a requirement to make a string value passed as initial caps. Can someone let me know what can be demerits using .capitalize method instead of substring the first position and convert it to uppercase?

Capitalize:

String RetName = '';
for (String token: inputString.split(' ')) 
  RetName += String.isEmpty(RetName) ? token.capitalize() : ' ' + token.capitalize();

Touppercase:

        if(!inputString.contains(' '))
        {
            String CapsName= inputString;
                  CapsName=(Capsname.substring(0,1)).toUppercase()+(CapsName.substring(1,CapsName.length()));
                  system.debug('CapsName@'+CapsName);
            return CapsName;
        }
        else
        {
             List<String> CapsName= inputString.split(' ');
             String RetName='';
                for(Integer i=0;i<CapsName.size();i++)
                {   if(CapsName[i]!='')  // Change 18-03-2015
                    CapsName[i] = (CapsName[i].substring(0,1)).toUppercase() +(CapsName[i].substring(1,CapsName[i].length()));
                    else Retname+=' ';  // Change 18-03-2015
                    if(i>0)
                    {
                        RetName=RetName+' '+CapsName[i];
                    }
                    else RetName=CapsName[i];
                }
                system.debug('Retname@'+Retname);
                return Retname;
         }
1
  • Using capitalize should be favoured. Most importantly, it clearly expresses programmer intent in a way that the subString approach doesn't, and it probably also executes faster as well not that that matters. Mar 23, 2015 at 13:58

1 Answer 1

2

I think it depends on the nature of the strings you'll be passing to your method as to which one is appropriate to use. If using substrings, one could add RegEx to capture certain types of groups that might need special attention. Let me cite a few examples and I think you'll readily see what I mean.

Lower Case Source          Capitalize()          subString

dr. john smith ii       Dr. John Smith Ii       Dr. John Smith II
                                              (Both i's get capitalized)
henry king jr           Henry King Jr           Henry King Jr  
                                         (option to insert Mr or end with Jr.)

mrs june carter         Mrs June Carter         Mrs. June Carter 
                                              (can add add . to Mrs)

dr alan pierce phd      Dr Alan Pierce Phd      Dr Alan Pierce PhD 
                                              (PhD or Ph.D are correct forms)

mrs. june carter        Mrs. June Carter        Mrs June Carter 
                                               (can remove periods)

dr and mrs john stone  Dr And Mrs John Stone   Dr and Mrs John Stone
                                                (and not capitalized)

robert alan jones      Robert Alan Jones       Robert A Jones  
                                              (can abbrev middle name & opt add a '.')

leo w smith cpa        Leo W Smith Cpa         Leo W Smith, CPA
                                            (comma then all caps CPA or C.P.A.)

As you can see, it would all depend on how much regex you wanted to add to customize your search strings to handle various situations you're aware you might encounter in your application that needs special attention. Clearly,

Capitalization code alone would be simpler, but whether it would serve your needs is entirely another matter. I don't know that speed is always a primary concern. I feel that obtaining the desired results is the first concern and optimizing your code to obtain those results is what follows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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