String s = "aaaljlfeakdsflkjsadjaefdsafhaasdasd"; // Number of occurrences int num = 0; //
Loop through each character , Determine whether it is a character a , If it is , accumulative frequency for ( // Enter code ) { // Get each character , Determine whether it is a character a if ( // Enter code )
{ // Cumulative statistics num++; } } System.out.println(" character a Number of occurrences :" + num);
1.
int num = 0; for ( int i=0;i<s.length();i++ ) { if ( s.charAt(i)=='a' ) {
num++; } }
2.bytes array
int num = 0; byte[] bytes=s.getBytes(); for ( int i=0;i<bytes.length;i++ ) {
if ( bytes[i]=='a' ) { num++; } }
3. Using arrays char
char[] c=s.toCharArray(); for ( int i=0;i<c.length;i++ ) { if ( c[i]=='a' ) {
num++; } }
4. utilize substring(startindex,endindex) String
for ( int i=0;i<s.length();i++ ) { String s1=s.substring(i,i+1); if (
s1.equals("a")) { // Notice this place can't be 'a'. num++; } }
Common ways to attach strings , Please keep in mind
Technology
Daily Recommendation