Let’s program for the sum of digits in String in Java.
package programnew; public class Test { public static void main(String[] args) { String s1= "fgls485k7"; int sumOfNumber = 0; char c[] =s1.toCharArray(); for(int i=0;i<s1.length();i++) { if(Character.isDigit(s1.charAt(i))) { sumOfNumber = sumOfNumber+Character.getNumericValue(s1.charAt(i)); } } System.out.println("sum of digit present in the string ="+sumOfNumber); } }
Output is:- sum of digit present in the string =24