2012-11-06 18 views

Odpowiedz

2

użyj regex [g] znaleźć char i policzyć wyniki jak poniżej:

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

Jeśli chcesz wielkość liter ma znaczenie liczby, należy użyć wyrażenia regularnego jak [gG] w wzorca .

6

chciałbym użyć Pattern i 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++; 
0

Można spróbować następujących czynności:

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

Spróbuj

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

Więcej o StringUtils można nauczyć się z pytaniem: How do I use StringUtils in Java?

+0

Skąd bierzesz 'StringUtils'? –

+2

Proszę sprawdzić to commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html – sunleo

4

Chociaż Regex będzie działał dobrze, ale tak naprawdę nie jest wymagany tutaj. Możesz to zrobić po prostu za pomocą for-loop, aby zachować count dla postaci.

Trzeba by przekształcić swój ciąg do tablicy char: -

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

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

lub można też zrobić to bez konwersji do charArray: -

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

Proszę sprawdzić, czy występuje niezgodność liczby. – sunleo

+0

@sunleo .. Dla jakiego kodu? Cóż, oboje działają dobrze. Właśnie to sprawdziłem. –

+0

@sunleo .. Proszę ich wypróbować. Obaj podają "3" jako liczbę w moim przypadku. –

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

Jako uwaga, jeśli 'char c' pochodzi od danych wprowadzonych przez użytkownika, to podejście jest podatne na wtrysk regex. (podobne do SQL injection) – Tuupertunut

0

You może przechodzić przez to i utrzymywać liczbę liter, które chcesz.

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

lub możesz użyć StringUtils, aby uzyskać liczbę.

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

wiem, że to i stare pytanie, ale istnieje możliwość, że nie została wysłuchana i to dość prosta-liner:

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

Myślę, że powinieneś dodać beginIndex i endIndex do podłańcucha –

1

jest to bardzo, bardzo stare pytanie, ale może to pomoże ktoś ("_")

można po prostu po prostu użyć tego kodu

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 = getCountOf(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; 
} 
0

użycie org.apache.c Pakiet ommons.lang3 do użytku Klasa StringUtils. pobierz plik jar i umieść go w folderze lib aplikacji internetowej.

int count = StringUtils.countMatches("engineering", "e"); 
Powiązane problemy