How to check whether a string is numeric using Apex in Salesforce?

How to check whether a string is numeric using Apex in Salesforce?

isNumeric() can be used to check whether a string is numeric using Apex in Salesforce.

Sample Code:

String str = 'abc';   

if ( str.isNumeric() ) {
        
    Integer intVal = Integer.ValueOf( str );
    System.debug( 'intVal value is ' + intVal );
    
} else {
    
    System.debug( 'String ' + str + ' is not numeric' );
    
}

Output:

Leave a Reply