'check how many times string contains character 'g' in eligible string

How we can check any string that contains any character how may time.... example: engineering is a string contains how many times 'g' in complete string



Solution 1:[1]

I know this is and old question, but there is an option that wasn't answered and it's pretty simple one-liner:

int count = string.length() - string.replaceAll("g","").length()

Solution 2:[2]

Try this

int count = StringUtils.countMatches("engineering", "e");

More about StringUtils can be learned from the question: How do I use StringUtils in Java?

Solution 3:[3]

I would use a Pattern and Matcher:

String string = "engineering";
Pattern pattern = Pattern.compile("([gG])"); //case insensitive, use [g] for only lower
Matcher matcher = pattern.matcher(string);
int count = 0;
while (matcher.find()) count++;

Solution 4:[4]

Although Regex will work fine, but it is not really required here. You can do it simply using a for-loop to maintain a count for a character.

You would need to convert your string to a char array: -

    String str = "engineering";
    char toCheck = 'g';
    int count = 0;

    for (char ch: str.toCharArray()) { 
        if (ch == toCheck) {
            count++;
        }
    }
    System.out.println(count);

or, you can also do it without converting to charArray: -

for (int i = 0; i < str.length(); i++) {
    if (str.charAt(i) == toCheck) {
        count++;
    }
}

Solution 5:[5]

String s = "engineering";
char c = 'g';
s.replaceAll("[^"+ c +"]", "").length();

Solution 6:[6]

Use regex [g] to find the char and count the findings as below:

    Pattern pattern = Pattern.compile("[g]");
    Matcher matcher = pattern.matcher("engineering");
    int countCharacter = 0;
    while(matcher.find()) {
        countCharacter++;
    }
    System.out.println(countCharacter);

If you want case insensitive count, use regex as [gG] in the Pattern.

Solution 7:[7]

use org.apache.commons.lang3 package for use StringUtils class. download jar file and place it into lib folder of your web application.

int count = StringUtils.countMatches("engineering", "e");

Solution 8:[8]

You can try Java-8 way. Easy, simple and more readable.

long countOfA = str.chars().filter(ch -> ch == 'g').count();

Solution 9:[9]

this is a very very old question but this might help someone ("_")

you can Just simply use this code

public static void main(String[] args){
    String mainString = "This is and that is he is and she is";
    //To find The "is" from the mainString
    String whatToFind = "is";
    int result = countMatches(mainString, whatToFind);
    System.out.println(result);
}

public static int countMatches(String mainString, String whatToFind){
    String tempString = mainString.replaceAll(whatToFind, "");
    //this even work for on letter
    int times = (mainString.length()-tempString.length())/whatToFind.length();
    
    //times should be 4
    return times;
}

Solution 10:[10]

You can try following :

String str = "engineering";
int letterCount = 0;
int index = -1;
while((index = str.indexOf('g', index+1)) > 0)
    letterCount++;
System.out.println("Letter Count = " + letterCount);

Solution 11:[11]

You can loop through it and keep a count of the letter you want.

public class Program {
    public static int countAChars(String s) {
        int count = 0;
        for(char c : s.toCharArray()) {
            if('a' == c) {
               count++;
            }
        }
        return count;
    }
}

or you can use StringUtils to get a count.

int count = StringUtils.countMatches("engineering", "e");

Solution 12:[12]

This is an old question and it is in Java but I will answer it in Python. This might be helpful:

string = 'E75;Z;00001;'

a = string.split(';')

print(len(a)-1)