I have to return specific characters from a field's data. The scenario is I have a Text field which has some data (a1h5D0000055ZLfETC-547561-12) and I need to find and return last 2 characters from the data. How to achieve the same.

  • How do you like to show, as a formula field or want to use in apex? Jan 31, 2017 at 5:54
  • I need to use this in apex Jan 31, 2017 at 5:55

2 Answers 2


You can use right() method.

String inputStr = 'a1h5D0000055ZLfETC-547561-12';
String resultStr = inputStr.right(2);

System.debug('inputStr  '+inputStr);
System.debug('resultStr  '+resultStr);


11:36:05:002 USER_DEBUG [3]|DEBUG|inputStr  a1h5D0000055ZLfETC-547561-12
11:36:05:002 USER_DEBUG [4]|DEBUG|resultStr  12

There are more String Methods.


Use substring method of String Class.

String str = 'a1h5D0000055ZLfETC-547561-12';
String resultantStr = str.substring(str.length()-2);

It will return 12 i.e. last 2 characters from str.

  • Thanks for the info... Will try and update if I face any issue. Jan 31, 2017 at 6:19
  • You will not face any issues, it's tested Jan 31, 2017 at 6:30

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.