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 Answers
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.
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. Commented Jan 31, 2017 at 6:19
-