[SalesForce] Ending position out of bound exception

I have below trigger that is getting below. I want just the letter after '-' to be in uppercase.

Compile Error: execution of BeforeUpdate caused by:
System.StringException: Ending position out of bounds: 1:
At Line :

{ guess = guess.substring(0,guess.indexOf('-'))+
guess.substring(guess.indexOf('-'),1).toUppercase() +
guess.substring(guess.indexOf('-')+1,guess.length()-1);

Can someone please help.

Trigger:

 trigger ACCOUNT_AFTER_INSUPD_NAME on Account (before insert , before update) {

    /*List<Account> acc = new List<Account>();
    acc = [SELECT Id,FirstName,LastName,Middle_vod__c FROM Account WHERE Id IN Trigger.oldMap.keySet() LIMIT 1];*/
    for(Account a: trigger.new)
    {

            if(a.Ispersonaccount==true)
            {
                    String f = a.FirstName;
                    List<String> FName= f.split(' ');
                    for(Integer i=0;i<FName.size();i++)
                    {
                            FName[i] = (FName[i].substring(0,1)).toUppercase() +(FName[i].substring(1,FName[i].length()).toLowercase());
                            if(i>0)
                            {
                                    a.FirstName=a.FirstName+' '+FName[i];
                            }
                            else a.FirstName=FName[i];
                    }

                    String m = a.Middle_vod__c;
                    if(m != null){
                            List<String> MName= m.split(' ');
                            for(Integer i=0;i<MName.size();i++)
                            {
                                    MName[i] = (MName[i].substring(0,1)).toUppercase() +(MName[i].substring(1,MName[i].length()).toLowercase());
                                    if(i>0)
                                    {
                                            a.Middle_vod__c=a.Middle_vod__c+' '+MName[i];
                                    }
                                    else a.Middle_vod__c=MName[i];

                            }
                    }

                    String l = a.LastName;
                    List<String> LName= l.split(' ');
                    for(Integer i=0;i<LName.size();i++)
                    {
                            LName[i] = (LName[i].substring(0,1)).toUppercase() +(LName[i].substring(1,LName[i].length()).toLowercase());
                            if(i>0)
                            {
                                    a.LastName=a.LastName+' '+LName[i];
                            }
                            else a.LastName=LName[i];

                    }

                    // For Mc'
                    String N = a.FirstName+' '+a.Middle_vod__c+' '+a.lastname;
                    String McName =a.Name;
                    List<String> FindName= N.split(' Mc');
                    for(Integer i=0;i<FindName.size();i++)
                    {  
                            FindName[i] = (FindName[i].substring(0,1)).toUppercase() +(FindName[i].substring(1,FindName[i].length()));
                            system.debug(FindName[i]+'1111');
                            if(i>0)
                            {
                                    McName=McName+' Mc'+FindName[i];
                                    system.debug(McName+'Line63');   
                            }
                            else McName=FindName[i];
                    }
                    a.Name=McName;

                    system.debug(a.Name+'Line63'); 
                    //
                    //update a;
            }

            // For Business Accounts
            else { 
                    String business = a.Name;
                    List<String> BName= business.split(' ');
                    for(Integer i=0;i<BName.size();i++)
                    {
                            BName[i] = (BName[i].substring(0,1)).toUppercase() +(BName[i].substring(1,BName[i].length()).toLowercase());
                            if(i>0)
                            {
                                    a.Name=a.Name+' '+BName[i];
                            }
                            else a.Name=BName[i];
                    } 
            } 

            system.debug(a.name);   

            // Special Characters for any Account    
            if(a.name.contains('-')) 
            {
                    String Acc=a.name;
                    system.debug(a.name); 
                    for (integer index = 0;index<1;index++)
                    {       
                            system.debug(Acc.substring(Acc.indexOf('-')+1,Acc.indexOf('-')+2).toUppercase()+'@@'); 
                            system.debug(Acc.substring(Acc.indexOf('-')+1,Acc.length())+'@@'); 
                            system.debug(Acc.substring(0,Acc.indexOf('-'))+'@@'); 
                            String X=Acc.substring(Acc.indexOf('-')+1,Acc.indexOf('-')+2).toUppercase();
                            Acc = Acc.substring(0,Acc.indexOf('-'))+ 
                                    Acc.substring(Acc.indexOf('-'),Acc.indexOf('-')+2).toUppercase() + 
                                    Acc.substring(Acc.indexOf('-')+2,Acc.length());
                            system.debug(Acc+'@315691'); 

                    }
                    a.Name=Acc;
            }

    }
}

Best Answer

You must have been confused by the exact syntax of .substring:

substring(Integer, Integer)

Returns a new String that begins with the character at the specified zero-based startIndex and extends to the character at endIndex - 1.

So the second substring is taking from first occurrence of - until index 1. That's not possible as 1 is smaller then the index of the first occurrence of -.

So you'd have to change your code to the following:

guess = guess.substring(0,guess.indexOf('-'))+ 
    guess.substring(guess.indexOf('-'),guess.indexOf('-')+2).toUppercase() + 
    guess.substring(guess.indexOf('-')+2,guess.length());
Related Topic