Skip to main content

Posts

Showing posts from February, 2019

Java: How to count the special characters in a String input without space?

We can go with String method and matching regex pattern to find the count of special char's in a string Input. Example: public class Testing {     public static void main(String[] args) {         String inputStr = "1234 Special Char count test example@$$%^^$&**() using String method @@#@";         System.out.println(inputStr.replaceAll("[0-9A-Za-z\\s]", ""));         System.out.println("Count:"+(inputStr.replaceAll("[0-9A-Za-z\\s]", "").length()));    } } Output: @$$%^^$&**()@@#@ Count:16

Java : How to find the counts of digits/numbers in a string using Java?

We can go with String method using regex pattern to find the count of valid digits in a string Input. Example: public class Test {       public static void main(String[] args) {           String inputStr = "1234 Number count test example using String method @@#@";         System.out.println("Digits Count:"+(inputStr.length()-inputStr.replaceAll("[0-9]", "").length()));           } Output: Digits Count:4