How to remove all non-numeric using Apex in Salesforce?

How to remove all non-numeric using Apex in Salesforce?

replaceAll() method can be used to remove all non-numeric present in a variable using Apex in Salesforce.

Sample Code:

String str = 'abc123xyz!@#$%^&*()_+';
String myOutStr = str.replaceAll( '[^0-9]', '' );
System.debug(
    ' Output String: ' + myOutStr
);

Leave a Reply