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 requiremnt i need to get numaric value from string field how can i get in apex
Eg: String name= 'Auto - New Business - Count;Premium - 500';
from above string name i nedd to fetch 500 how can i do that 
i used one method 
String name= 'Auto - New Business - Count;Premium - 500';
String substr = name.substringAfter('-');
system.debug('substr'+substr);
but hear i am getting - New Business - Count;Premium - 500' total value but i need only 500 .
how can i do that some one help me please 

Thank you 
Surender Reddy

 

CharuDuttCharuDutt
Hii Surender
Try Below Code
String invalidNumbers = '[^0-9]';  
String str = 'Auto - New Business - Count;Premium - 500';  
system.debug( 'Only numbers - ' + str.replaceAll( invalidNumbers, '' ) );
Please Mark It As best Answer If it Helps
Thank You!
Vishesh kant Aggarwal 17Vishesh kant Aggarwal 17

Hi Surender,

The below code will serve your purpose.

// string contains numbers
String str = 'Auto - New Business - Count;Premium - 500';

// extract digits only from strings
String numberOnly = str.replaceAll('[^0-9]', '');

// print the digitts
System.debug(numberOnly);
Please mark this as the correct answer for others.
Thanks in advance !!
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the solution.

String notValidNumber= '[^0-9]';  
String str = 'Auto - New Business - Count;Premium - 500';  
system.debug( 'Only numbers - ' + str.replaceAll( notValidNumber, '' ) );  

or

String formattedStr = 'Auto - New Business - Count;Premium - 500';
String[] strArr = formattedStr.split('-');
  
system.debug('myNumber::'+strArr[3]);

Please mark it as the Best Answer if it helps you

Thank You

vfdhgfr bfdhbgfvfdhgfr bfdhbgf
If you are looking to get number value from string the I think you need to learn about it from here (https://cosmichairs.com/) and try to apply this code on your task.