Flash cards
Review the key moves
1/4
Core idea
What is the main idea behind Java How To Count Digits in a String?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
1Quick choice
Which statement best captures the main point of this lesson?
2Fill blank
Complete the missing token from the example code.
___ text = "ExampleSite was founded in 1998";3Order
Put the learning moves in the order that makes the concept easiest to apply.
Explanation: We loop through each character of the string.
Go through each character and count how many are digits (0-9).
Count Digits in a String
Count Digits in a String
Go through each character and count how many are digits (0-9).
Example
String text = "ExampleSite was founded in 1998";
int count = 0;
for (char c : text.toCharArray()) {
if (Character.isDigit(c)) {
count++;
}
}
System.out.println("Digits: " + count);Explanation: We loop through each character of the string. The method Character.isDigit() checks if the character is a digit. For the example above, the program finds 5 digits.