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
ForceLoverForceLover 

Removing non numeric data from a field

Hi Everyone ,

                       i have two fields (field1,field2) and i need to remove all non numeric characters from field1 and has to store the remaining numeric data  in field2 how can i achieve this....suggest me in this regard

SLockardSLockard

Here is some logic that separates alpha characters from numeric ones, hope it helps you.

 

String before = '123AbCgoya1Me';
String alpha = '';
String numeric = '';

List<String> chars = before.split('|');
for (String s : chars)
{
    if (s.isAlpha())
    {
        alpha += s;
    }
    else
    {
        numeric += s;
    }
}

system.debug(alpha);
system.debug(numeric);

 

Naidu PothiniNaidu Pothini
String field1 = 'Test123';

String field2 = field1.replaceAll('[^0-9]', '');

 

 

SLockardSLockard

Just missing one part

String field1 = 'Test123';
String field2 = field1.replaceAll('[^0-9]', '');

field1 = field1.replaceAll('[0-9]', '');

system.debug(field1);
system.debug(field2);

 

DMendozaDMendoza
To remove all non-numeric characters from a string, such as a phone number, I use 
lead.Phone = lead.Phone != null ? lead.Phone.replaceAll('^\\D','') : null;
CHAKRA GADIGE 7CHAKRA GADIGE 7
To Extract Numarics from String :
String str = '(667) 788  455435dsdsdasdasd 445345...4/6fsdsdds';
 str = str.replaceAll('\\D', '');
System.debug('Final integer  : '+str );
// output : 66778845543544534546