Java implementation of statistical string uppercase letters, lowercase letters and the number of times the method example


In this paper, an example is given to illustrate how Java realizes the statistics of the occurrence times of capital letters, lowercase letters and numbers in strings. Share for your reference, as follows:

public class TestSubstring {
  public static void main(String[] args) {
  // Statistical string, capital letters, lowercase letters, the number of occurrences
  public static void getCount(String str) {
    int upper = 0;
    int lower = 0;
    int digi = 0;
    for (int i = 0, len = str.length(); i < len; i++) {
      char c = str.charAt(i);
      if (c >= 'A' && c <= 'Z') {
      } else if (c >= 'a' && c <= 'z') {
      } else if (c >= '0' && c <= '9') {
    System.out.println ("capital letters:"+upper); //capital letters: 13
    System.out.println ("lowercase:"+lower); //lowercase: 9
    System.out.println ("Number:"+digi);//Number:20

Operation results:

PS: Here we recommend two very convenient statistical tools for your reference.

Online Word Number Statistics Tool:

Online Character Statistics and Editing Tool:

Readers interested in more about Java algorithms can see the topics on this site: Java Data Structure and Algorithms Tutorial, Summary of Java Operating DOM Node Skills, Summary of Java File and Directory Operating Skills, and Summary of Java Caching Operating Skills.

I hope this article will be helpful to the design of Java program.

Recommended Today

Manjaro uses SS method (requires nodejs environment)

Just installed manjaro, need to installshadowsocks-qt5+Proxy SwitchyOmega, but the latest Chrome has long blocked the installation of non Google store CRX channels. Here is my solution, which is troublesome but usable. If you are familiar with NPM commands in nodejs, you won’t find it troublesome, because nodejs is used for compilation. Preparation environment: A VPS […]