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
Surender reddy SalukutiSurender reddy Salukuti 

How can i get number value from string ?

Hi all,

i have one requerement ,
how we can get Number values from string .
eg :string str=' Auto - New Business - Count;Premium - 300 - 400';
i need to get 300 and 400 values from string and need to replace that values with other values 

can any one help me on this .

Thanks ,
Surender Reddy 


 

SwethaSwetha (Salesforce Developers) 
HI Surender,
Incase there is no pattern of occurrence like the number is seen after x letters, you can go with REGEX .

You can replace all non number characters with empty characters, and you will have only numbers left. This regex says: any character a-z or A-Z or dash
String recordName = 'RO-W1-445567'; String regex = '[a-zA-Z]{1,}|\\-'; String recordNumber = recordName.replaceAll(regex, ''); System.debug(recordNumber);
See reference : https://salesforce.stackexchange.com/questions/42003/how-to-extract-a-number-from-a-string that has other approaches too.

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
mukesh guptamukesh gupta
Hi Surender,

Please use tested code:-
 
string str1 = 'Auto - New Business - Count;Premium - 300 - 400';
String[] val1 = str1.split('-');
System.debug('val1-- '+val1[3]  +'val2-- '+val1[4]  );

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
ravi soniravi soni
hy surender,
you can use split function if you are confirm that before number (Dash ( - ) ) will come then you can use below code.
string str1 = 'Auto - New Business - Count;Premium - 300 - 400';
String[] val1 = str1.split('-');
for(string s : val1 ){
    if(s.trim().isNumeric()){
        system.debug(s);//find only numeric value
    }    
}

don't forget to mark it as best answer if it is helpful to you.
Thank you


 
ravi soniravi soni
hy Surender,
let us know what is your status our code is working fine or not. if yes, then don't forget to mark it as best answer.
thank you