Basics: Keep in mind the various methods of string manipulation:
? ??
? String s = "AAALJLFEAKDSFLKJSADJAEFDSAFHAASDASD"; Number of occurrences int num = 0; Loop through each character to determine if it is a character a, if yes, the number of increments for ( //Input code ) { //To get each character, to determine whether the character a if ( //Input code ) { //Cumulative statistics Count num++; } } System.out.println ("The number of occurrences of character a:" + num);?
1.
? int num = 0; for ( int i=0;i<s.length (); i++ ) { if ( s.charat (i) = = ' A ') { num++; } }?
2. Using the properties of an array of bytes arrays:
? int num = 0; Byte[] Bytes=s.getbytes (); for ( int i=0;i<bytes.length;i++ ) { if (bytes[i]== ' a ') { num++; } }?
3. Using the array char
? Char[] C=s.tochararray (); for ( int i=0;i<c.length;i++ ) { if ( c[i]== ' a ' ) { num++; } }?
4. Using the substring (startindex,endindex) string
? for ( int i=0;i<s.length (); i++ ) { String s1=s.substring (i,i+1); if ( s1.equals ("a")) { //Note this place cannot be ' a '. num++; } }?
The
Java implementation iterates through each letter of a string (summary)