24

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

user1802156
  • 273
  • 1
  • 2
  • 3
  • 1
    Convert it to character array and loop through it checking for matches. Check out [java.util.String](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html) – MadProgrammer Nov 06 '12 at 06:25

12 Answers12

56

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()
chrisonline
  • 6,949
  • 11
  • 42
  • 62
37

Try this

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

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

Community
  • 1
  • 1
sunleo
  • 10,589
  • 35
  • 116
  • 196
8

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++;
doublesharp
  • 26,888
  • 6
  • 52
  • 73
7

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++;
    }
}
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
4
String s = "engineering";
char c = 'g';
s.replaceAll("[^"+ c +"]", "").length();
adjablon
  • 502
  • 4
  • 9
  • As a note, if `char c` comes from user input, this approach is vulnerable to regex injection. (similar to SQL injection) – Tuupertunut Dec 31 '16 at 00:37
4

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");
3

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.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
1

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;
}
abood nasr
  • 11
  • 1
  • 3
1

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

long countOfA = str.chars().filter(ch -> ch == 'g').count();
S.P.
  • 121
  • 1
  • 2
0

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);
Azodious
  • 13,752
  • 1
  • 36
  • 71
0

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");
0

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)
the Tin Man
  • 158,662
  • 42
  • 215
  • 303