1

I have a requirement to make string values passed as init caps. How to check whether the element I am capitalizing by toUppercase() is an alphabet or not? Please find my code snippet here.

Snippet:

        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()));  //CHECK HERE IF THE 1ST ELEMENT IS ALPHABET 
                    else Retname+=' ';  // Change 18-03-2015
                    if(i>0)
                    {
                        RetName=RetName+' '+CapsName[i];
                    }
                    else RetName=CapsName[i];
                }
                system.debug('Retname@'+Retname);
                return Retname;
         }

1 Answer 1

2

You can use a string method isAlpha():

Returns true if all characters in the current String are Unicode letters only; otherwise, returns false.

String.isAlpha();

You must log in to answer this question.

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