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

Time:2019-8-9

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) {
    getCount("adsJKJ3K21AfaAD134F13241d134134s141faAAFDF");
  }
  // 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') {
        upper++;
      } else if (c >= 'a' && c <= 'z') {
        lower++;
      } else if (c >= '0' && c <= '9') {
        digi++;
      }
    }
    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:
http://tools.jb51.net/code/zishutongji

Online Character Statistics and Editing Tool:
http://tools.jb51.net/code/char_tongji

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 […]