function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfg1sfg1 

How to check whether field value is numeric or Alphnumeric in apex

how to find field value whether it is numeric or Alphnumeric in apex.
I have requirements like if FIELD1 = 123, FIELD2=456, display FIELD1 value in OUTPUTFIELD=123, if FIELD1=A123 or 123A, FIELD2=456 then display FIELD2 value in OUTPUTFIELD=456. 
Please guide me.
Shruti SShruti S
You can use isNumeric() method provided by String class in Apex. It returns a true if the string is a number and returns a false if the string contains an alphabet in it. Here is the code for your above use case -
if( field1__c.isNumeric() && field2__c.isNumeric() ) {
    outputfield__c = field1__c;
}
else if( !field1__c.isNumeric() && field2__c.isNumeric() ) {
    outputfield__c = field2__c;
}
Junaid AnsariJunaid Ansari

It will give false for -ve values.
Exp :
String str1 = '-12';
String str2 = '12';
System.debug('is numereci >> '+str1.isNumeric());   //false
System.debug('is numereci >> '+str2.isNumeric());   //true