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
-
1Convert 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 Answers
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()

- 6,949
- 11
- 42
- 62

- 682
- 5
- 2
-
1
-
Thanks, this is definitely a lot easier than having to download the apache commons lang API. – Krusty the Clown Apr 17 '18 at 21:06
Try this
int count = StringUtils.countMatches("engineering", "e");
More about StringUtils can be learned from the question: How do I use StringUtils in Java?
-
-
2Please check this commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html – sunleo Nov 06 '12 at 06:37
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++;

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

- 209,639
- 45
- 409
- 525
-
-
@sunleo.. For which code? Well, both of them are working fine. Just checked it. – Rohit Jain Nov 06 '12 at 06:37
-
@sunleo.. Please try them. They both are giving `3` as count in my case. – Rohit Jain Nov 06 '12 at 06:41
-
both are working fine thank you.Sorry for inconvenience.Eclipse not refreshed the latest class. – sunleo Nov 06 '12 at 06:43
-
String s = "engineering";
char c = 'g';
s.replaceAll("[^"+ c +"]", "").length();

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

- 71
- 1
- 5
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.

- 33,927
- 6
- 63
- 73
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;
}

- 11
- 1
- 3
You can try Java-8 way. Easy, simple and more readable.
long countOfA = str.chars().filter(ch -> ch == 'g').count();

- 121
- 1
- 2
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);

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

- 158,662
- 42
- 215
- 303

- 51
- 3