Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have an apex class which iterates through a word and capitalises every first letter, however we can't use this on the name field in accounts because accounts with acronyms or initials get inappropriately cased.

I tried to add an if statement that determines if the whole word is already upper case, but it didn't work because through debug statements I found out that Apex was equating "test" and "TEST" as the same.

Does anyone have a workaround for this or is there a way of doing this that I'm missing?

public static String capitaliseFirstLetters(String name) 
{
    name = name.toLowerCase();
    String result = '';
    //List of separators after which we should capitalize letter
    String[] separatorList = new String[]{' ','-'};
    if (name != null && name != '') 
    {
        result = name;
        for(String separator : separatorList)
        {
            String tmpResult = '';
            for (String iter : result.split('[' + separator+']+')) 
            {        
                System.debug('Begin iter loop. tmpResult: ' + tmpResult + '. iter: ' + iter);         
                if (iter != null && iter != '') 
                {
                    System.debug('Iter: ' + iter + ' is not equal to null AND not blank.');
                    if (iter != iter.toUpperCase())
                    {
                        System.debug('Iter: ' + iter + ' is not equal to an upper case version of itself: ' + iter.toUpperCase() + '.');
                        if (iter.length() > 1 ) 
                        {   
                            System.debug('Iter is bigger than 1.');
                            tmpResult += iter.substring(0,1).toUpperCase() + iter.substring(1,iter.length()) + separator;
                        }
                        else 
                        {
                            System.debug('Iter is not bigger than 1.');
                            tmpResult += iter.substring(0,1).toUpperCase() + separator;
                        }
                    }
                    else
                    {
                        System.debug('Iter: ' + iter + ' is equal to an upper case version of itself: ' + iter.toUpperCase() + '.');
                        tmpResult += iter + separator;
                    }
                }
                else
                {
                    System.debug('Iter is either equal to null or blank.');
                }
                System.debug('End iter loop. tmpResult: ' + tmpResult + '. iter: ' + iter);   
            }
            result = tmpResult.substring(0, tmpResult.length() - 1);
        }
    }
    return result;
}
share|improve this question

2 Answers 2

up vote 7 down vote accepted

Can you use the String .equals method, as this is case sensitive

http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_string.htm

String myString1 = 'Abc';
String myString2 = 'abc';
Boolean result = myString1.equals(myString2);
System.assertEquals(result, false);

Note that the == operator also performs String comparison, but is case-insensitive to match Apex semantics. (== is case-sensitive for ID comparison for the same reason.)

share|improve this answer
    
I found the reference for these string operations almost immediately after I posted the question but thank you. Also noticed that the function sets the whole word to lower case at the start anyway :/ –  Adam Mar 25 at 16:17

Try using String methods.

if (!iter.equals(iter.toUpperCase()))
share|improve this answer

Your Answer

 
discard

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.