0

My code in unable to split hyphen. PLease have a look to my statement if I am wrong here.

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];
  }

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

     }
     else a.Name=FindName[i+1];




  }
   //
  //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];
     } 
         } 

         // Special Characters for any Account    

   String Spl = a.Name;
   //name = name.split('-')[0]+'*';
  List<String> SpclName= Spl.split('[-]');
  for(Integer j=0;j<SpclName.size()-1;j++)
  {
    SpclName[j+1] = (SpclName[j+1].substring(0,1)).toUppercase() +(SpclName[j+1].substring(1,SpclName[j+1].length()));
     if(j>0)
     {
     a.Name=a.Name+' '+SpclName[j+1];
     }
     else a.Name=SpclName[j+1];
   }   


 }
}
4
  • What exactly goes wrong? Do you get any errors or do you just not get the expected result?
    – Folkert
    Commented Dec 13, 2014 at 10:36
  • Yes I am not getting expected Name field value. Like I get "Saikat Mclean Xyz-yy" but I expect "Saikat Mclean Xyz-Yy" Commented Dec 13, 2014 at 10:40
  • Then I'd suspect that the split isn't the problem, but the for loop after it is.
    – Folkert
    Commented Dec 13, 2014 at 10:50
  • Are you expecting just a single hyphen in the string? Commented Dec 15, 2014 at 7:39

2 Answers 2

1

Your problem is here in the for loop:

 for(Integer j=0;j<SpclName.size()-1;j++)

If your list size is 1 then it is (j=0;j<0;j++) which loops 0 times. Change it to <= or get rid of the -1.

0

Are you sure your data contains [-]? Because you aren't splitting on just a hyphen but on a hyphen contained in square brackets.

You should also define what data is in the List resulting from the split. Try this:

String Spl = a.Name;
List<String> SpclName = Spl.split('-');

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

6
  • Hi Folkert! This was what I am trying to do. I found somewhere in google that need to introduce [ ]. But my objective is to split hyphen. I am giving my whole code here. Commented Dec 13, 2014 at 10:31
  • 3
    The brackets [ ] are used for character classes, a feature of regular expressions. From this standpoint, for this question, there is no difference between [-] and -, but is generally better to get in the habit of, because split('[-;]') (split on either - or ;) is not the same as split('-;') (split on a - followed by a ;).
    – sfdcfox
    Commented Dec 13, 2014 at 10:47
  • Ah, another thing learned. It's been a while since I used regular expressions. This and the addition of the full code kind of invalidates my answer, but I'll keep the answer there so your comment stays.
    – Folkert
    Commented Dec 13, 2014 at 10:52
  • Hi sfdcfox! I tried ('-;') but not working. Can you please let me know the root cause here? Commented Dec 13, 2014 at 11:39
  • @SFDC_GEEK '-;' was just an example to show the difference between split('-;') and split(['-;']) to demonstrate the effect of the brackets.
    – Folkert
    Commented Dec 13, 2014 at 12:32

You must log in to answer this question.

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