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