0

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.

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

2 Answers 2

1

You can use right() method.

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

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

Output:

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.

0

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.

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

You must log in to answer this question.

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