This article describes the method of java to implement statistics on the number of characters and substrings in strings. Share it for your reference, as follows:
Here, Java is used to count the number of characters (including numbers, uppercase letters, lowercase letters and other characters) in a string, as well as the number of substrings of the string.
The operation renderings are as follows:
The specific code is as follows:
import java.util.Scanner;public class Counter { static Scanner scanner = new Scanner(System.in); public static void count(String s) { int low, upper, num, others; low = upper = num = others = 0; for (int i = 0; i < s.length(); i++) { if (Character.isDigit(s.charAt(i))) { num++; continue; } if (Character.isLowerCase(s.charAt(i))) { low++; continue; } if (Character.isUpperCase(s.charAt(i))) { upper++; continue; } else { others++; } } System.out.println(" The number of uppercase letters is: " + upper + "/n The number of lowercase letters is: " + low+ "/n The number of numbers is: " + num + "/n The number of other characters is: " + others); } public static void subCounter(String str1, String str2) { int counter = 0; for (int i = 0; i <= str1.length() - str2.length(); i++) { if (str1.substring(i, i + str2.length()).equalsIgnoreCase(str2)) { counter++; } } System.out.println("The number of substrings is: " + counter); } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Please enter a string:"); String string = scanner.nextLine(); count(string); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Query how many substrings str exist in this string. System.out.println("Please enter a substring you want to query:"); String str = scanner.nextLine(); subCounter(string, str); }}PS: Here are a few online character statistics tools for your reference:
Online word count tool:
http://tools.VeVB.COM/code/zishutongji
Online character statistics and editing tools:
http://tools.VeVB.COM/code/char_tongji
I hope this article will be helpful to everyone's Java programming.