Java Program to Count Number of Digits in a String Last Updated : 02 Nov, 2022 Comments Improve Suggest changes Like Article Like Report The string is a sequence of characters. In java, objects of String are immutable. Immutable means that once an object is created, it's content can't change. Complete traversal in the string is required to find the total number of digits in a string. Examples: Input : string = "GeeksforGeeks password is : 1234" Output: Total number of Digits = 4 Input : string = "G e e k s f o r G e e k 1234" Output: Total number of Digits = 4Approach: Create one integer variable and initialize it with 0.Start string traversal.If the ASCII code of character at the current index is greater than or equals to 48 and less than or equals to 57 then increment the variable.After the end of the traversal, print variable.Below is the implementation of the above approach: Java // Java Program to Count Number of Digits in a String public class GFG { public static void main(String[] args) { String str = "GeeksforGeeks password is : 1234"; int digits = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) >= 48 && str.charAt(i) <= 57) digits++; } System.out.println("Total number of Digits = " + digits); } } OutputTotal number of Digits = 4 Time Complexity: O(N), where N is the length of the string. Approach : Using isDigit() method Java // Java Program to Count Number of Digits in a String public class GFG { public static void main(String[] args) { String str = "GeeksforGeeks password is : 1234"; int digits = 0; for (int i = 0; i < str.length(); i++) { if (Character.isDigit(str.charAt(i))) digits++; } System.out.println("Total number of Digits = " + digits); } } OutputTotal number of Digits = 4 Approach : Using contains() method and ArrayList Java // Java Program to Count Number of Digits in a String import java.lang.*; import java.util.*; public class Main{ public static void main(String[] args) { String str = "GeeksforGeeks password is : 1234"; String num="0123456789"; ArrayList<Character> numbers= new ArrayList<>(); for(int i=0;i<num.length();i++) { numbers.add(num.charAt(i)); } int digits = 0; for (int i = 0; i < str.length(); i++) { if (numbers.contains(str.charAt(i))) digits++; } System.out.println("Total number of Digits = "+ digits); } } OutputTotal number of Digits = 4 Comment More infoAdvertise with us Next Article Java Program to Count Number of Digits in a String C chetanjha888 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-String-Programs +1 More Practice Tags : Java Similar Reads Java Program to Count Number of Vowels in a String In java, the string is a sequence of characters and char is a single digit used to store variables. The char uses 2 bytes in java. In java, BufferedReader and InputStreamReader are used to read the input given by the user from the keyboard. Then readLine() is used for reading a line. The java.io pac 4 min read Java Program to Convert Long to String The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String. Given a Long number, the task is to convert it into a 4 min read Java Program to Convert String to Long To convert a String to Long in Java, we can use built-in methods provided by the Long class. In this article, we will learn how to convert String to Long in Java with different methods. Example:In the below example, we use the most common method i.e. Long.parseLong() method to convert a string to a 3 min read How to Convert a String to an Numeric in Java? In Java programming, there are situations in which we must convert a string of numeric characters into one of the following data types: double, float, long, or int. To execute arithmetic operations and other numerical calculations, this conversion is necessary. We will look at how to convert a strin 2 min read How to Check if a String Contains only Digits in Java? In Java, to check if a string contains only digits, we can use various methods such as simple iteration, regular expressions, streams, etc.Example:The example below uses the Character.isDigit() method to check each character of the string to ensure all characters are digits. This is the most simple 3 min read Like