This article shares with you the java string traversal and the specific code of various characters in the java statistics string for your reference. The specific content is as follows
1. Requirements: Get every character in the string
analyze:
A: How can I get every character?
char charAt(int index)
B: How do I know how many characters there are?
int length()
public class StringTest { public static void main(String[] args) { // Define the string String s = "helloworld"; for (int x = 0; x < s.length(); x++) { // char ch = s.charAt(x); // System.out.println(ch); // Just output, I directly output System.out.println(s.charAt(x)); } }}2. Requirements: count the number of times when uppercase, lowercase, and numeric characters appear in a string. (No other characters are considered)
For example:
"Person1314Study"
analyze:
A: First define three variables bignum, samllnum, numbersum
B: traversal of array for(), length(), charAt()
C: determine which bignum of each character belongs to three variables: (ch>='A' && ch<='Z')
smallnum:(ch>='a' && ch<='z')
numbersum:(ch>='0' && ch<='9')
D: Output
public class StringTest3 { public static void main(String[] args) { //Define a string String s = "Person1314Study"; //Define three statistical variables int bignum = 0; int smallnum = 0; int numbernum = 0; //Tranquility through the string and get each character. for(int x=0;x<s.length();x++){ char ch = s.charAt(x); //Judge which type of the character belongs to if(ch>='A' && ch<='Z'){ bignum++; } else if(ch>='a' && ch<='z'){ smallnum++; } else if(ch>='0' && ch<='9'){ numbernum++; } } //Output result. System.out.println("contains"+bignum+"caps"); System.out.println("contains"+smallnum+"lowercase letters"); System.out.println("contains"+numbernum+"numbernum); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.