The string class has a number of fetch methods, which can be viewed in the API documentation. For the acquisition method, a small case is given.
/* Requirements: traverse to get every character in a string * Analysis: Use two methods: char charAt (int index) to get the string specifies the index of the character int length () to get the length of the string */ public class Stringtest {public static void main (string[] args) {//define string s = "HelloWorld"; Think, to get each index and more efficient, with a for loop improvement. for (int x = 0; x < s.length (); + +) {//char ch = s.charat (x)///SYSTEM.OUT.PRINTLN (CH)///output only, I directly output System.out.pri NT (S.charat (x));}}}
Then write a small case:
/* Requirements: Counts the number of uppercase characters, lowercase alphabetic characters, and number characters that occur in a string. (regardless of other characters) * Example: * "Hello123world" * Result: * Uppercase characters: 2 * lowercase characters: 8 * Numeric character: 3 * * Analysis: * Premise: String to exist * A: Define three statistical variables * Bigcount=0 * smallcount=0 * numbercount=0 * B: Iterate through the strings to get each character. * Length () and charat () combine * C: Determine whether the character belongs to that type of * large: bigcount++ * Small: smallcount++ * Number: numbercount++ *d: Output result. * */public class StringTest2 {public static void main (string[] args) {//define string s = "Hello123world";//define three statistical variables int BI Gcount = 0;int Smallcount = 0;int Numbercount = 0;//traverses the string to get each character. for (int x=0; x<s.length (); x + +) {Char ch = s.charat (x);//Determine if the character belongs to that type of if (ch>= ' a ' && ch<= ' z ') { smallcount++;} else if (ch>= ' A ' && ch<= ' Z ') {bigcount++;} else if (ch>= ' 0 ' && ch<= ' 9 ') {numbercount++;}} Output results. System.out.println ("Capital letter" +bigcount+ "one"); SYSTEM.OUT.PRINTLN ("Small Letter" +smallcount+ "one"); System.out.println ("number" +numbercount+ "a");}}
There is actually a simpler way to get the number of uppercase and lowercase numbers. It will be written later.
A small case of string class such as the first season of getting Started with Java