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

Implementation of PHP Facades

Example <?php class RealRoute{ public function get(){ Echo’Get me’; } } class Facade{ public static $resolvedInstance; public static $app; public static function __callStatic($method,$args){ $instance = static::getFacadeRoot(); if(!$instance){ throw new RuntimeException(‘A facade root has not been set.’); } return $instance->$method(…$args); } // Get the Facade root object public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); } protected […]