There are three ways.

  1. Using charAt – Traditional For Loop
  2. Using toCharArray – Enhanced For Loop
  3. Using replaceAll – Efficient Way without Looping

NO 1 Using charAt – Tutorial

package com.javaindetail.PatternTutorial;

public class repeatedCharacters {
  public static void main(String args[]){
    String str = "javaindetail.com";
    
    int counter = 0;
    for( int i=0; i<str.length(); i++ ) {
        if( str.charAt(i) == 'a' ) {
            counter++;
        } 
    }
    System.out.println(counter);
  }
}

NO 2 Using toCharArray – Tutorial

package com.javaindetail.PatternTutorial;

public class repeatedCharacters {
  public static void main(String args[]){
    String str = "javaindetail.com";
    
    int counter = 0;
    for(char c : str.toCharArray()) {
        if( c == 'a' ) {
            counter++;
        } 
    }
    System.out.println(counter);
  }
}

NO 3 Using replaceAll – Tutorial

package com.javaindetail.PatternTutorial;

public class repeatedCharacters {
  public static void main(String args[]){
    String str = "javaindetail.com";
    
    int counter = str.length() - str.replaceAll("a", "").length();
    
    System.out.println(counter);
  }
}