The traversal of the Java string and the statistics of the various characters in the string

Source: Internet
Author: User

1. Requirements: Get every character in a string

Analysis:
A: How can I get every single character?
char charAt (int index)
B: How do I know how many characters there are?
int Length ()

 Public classStringtest { Public Static voidMain (string[] args) {//Defining StringsString s = "HelloWorld"; for(intx = 0; x < S.length (); X + +) {            //char ch = s.charat (x); //System.out.println (CH); //just the output, and I'm going to output it directly.System.out.println (S.charat (x)); }    }}

2. Requirements: Count the number of uppercase characters, lowercase alphabetic characters, and number characters that appear in a string. (No other characters are considered)

Example:
"Person1314study"
Analysis:
A: Define three variables first
Bignum, Samllnum, numbersum
B: Traversal of the array
for (), lenght (), charAt ()
C: Determine which of the three variables each character belongs to.
Bignum: (ch>= ' A ' && ch<= ' Z ')
Smallnum: (ch>= ' a ' && ch<= ' z ')
Numbersum: (ch>= ' 0 ' && ch<= ' 9 ')
D: Output

 Public classStringTest3 { Public Static voidMain (string[] args) {//Define a stringString s = "Person1314study"; //define three statistical variables        intBignum = 0; intSmallnum = 0; intNumbernum = 0; //iterate through the strings to get each character.          for(intX=0;x<s.length (); x + +){            CharCH =S.charat (x); //determine whether the character belongs to that type.            if(ch>= ' A ' && ch<= ' Z ') {Bignum++; }            Else if(ch>= ' A ' && ch<= ' z ') {Smallnum++; }            Else if(ch>= ' 0 ' && ch<= ' 9 ') {Numbernum++; }        }        //output results. System.out.println ("contains" +bignum+ "caps"); System.out.println ("contains" +smallnum+ "lowercase letters"); System.out.println ("contains" +numbernum+ "number"); }}

The traversal of the Java string and the statistics of the various characters in the string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.