I need to find the last occurrence of the character comma for retrieve last word of a text field.


Text field: Discovery 3.0 SDV6 188KW Diesel (HP) SE, Italy ITA, HSVH/350HG, **2012**

The formula results get last word: 2012

  • after last comma always you will get Year ie. 2012 or 2015 or 2016 ?
    – Ratan Paul
    Feb 12 '16 at 16:10
  • the year is not the same for all records
    – maidenfp
    Feb 12 '16 at 16:11
  • Means it will only last 4 character correct?
    – Ratan Paul
    Feb 12 '16 at 16:14
  • It may be that the year also contains other characters. I'm interested in all the characters after the last comma
    – maidenfp
    Feb 12 '16 at 16:15
  • 1
    this is a very fragile solution as I'm sure you realize as it is dependent on # of commas in text field; apex is more robust (a before insert/update trigger)
    – cropredy
    Feb 12 '16 at 17:40

Query the Object and your text field and use the below code snippet. This should work for you.

List<String> lstStr = Your_Object_name.Your_File.Name.split(',');
system.debug('--Your desired Result--'+lstStr[lstStr.size()-1]);

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.