Occurrences of substring in a string

2018-12-31 05:59发布

Why is the following algorithm not halting for me? (str is the string I am searching in, findStr is the string I am trying to find)

String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
int count = 0;

while (lastIndex != -1) {
    lastIndex = str.indexOf(findStr,lastIndex);

    if( lastIndex != -1)
        count++;

    lastIndex += findStr.length();
}

System.out.println(count);

标签: java string
24条回答
回忆,回不去的记忆
2楼-- · 2018-12-31 06:47

Do you really have to handle the matching yourself ? Especially if all you need is the number of occurences, regular expressions are tidier :

String str = "helloslkhellodjladfjhello";
Pattern p = Pattern.compile("hello");
Matcher m = p.matcher(str);
int count = 0;
while (m.find()){
    count +=1;
}
System.out.println(count);     
查看更多
ら面具成の殇う
3楼-- · 2018-12-31 06:51

If you need the index of each substring within the original string, you can do something with indexOf like this:

 private static List<Integer> getAllIndexesOfSubstringInString(String fullString, String substring) {
    int pointIndex = 0;
    List<Integer> allOccurences = new ArrayList<Integer>();
    while(fullPdfText.indexOf(substring,pointIndex) >= 0){
       allOccurences.add(fullPdfText.indexOf(substring, pointIndex));
       pointIndex = fullPdfText.indexOf(substring, pointIndex) + substring.length();
    }
    return allOccurences;
}
查看更多
不流泪的眼
4楼-- · 2018-12-31 06:52

This below method show how many time substring repeat on ur whole string. Hope use full to you:-

    String search_pattern="aaa";
    String whole_pattern=""aaaaaababaaaaaa;
    int j = search_pattern.length();
    for (int i = 0; i < whole_pattern.length() - j + 1; i++) {

        String str1 = whole_pattern.substring(i, j + i);

        System.out.println("sub string loop " + i + " => " + str1);

        if (str1.equals(search_pattern)) {
            Constants.k++;
        }

    }
查看更多
余生无你
5楼-- · 2018-12-31 06:52
public static int getCountSubString(String str , String sub){
int n = 0, m = 0, counter = 0, counterSub = 0;
while(n < str.length()){
  counter = 0;
  m = 0;
  while(m < sub.length() && str.charAt(n) == sub.charAt(m)){
    counter++;
    m++; n++;
  }
  if (counter == sub.length()){
    counterSub++;
    continue;
  }
  else if(counter > 0){
    continue;
  }
  n++;
}

return  counterSub;

}

查看更多
几人难应
6楼-- · 2018-12-31 06:53

A lot of the given answers fail on one or more of:

  • Patterns of arbitrary length
  • Overlapping matches (such as counting "232" in "23232" or "aa" in "aaa")
  • Regular expression meta-characters

Here's what I wrote:

static int countMatches(Pattern pattern, String string)
{
    Matcher matcher = pattern.matcher(string);

    int count = 0;
    int pos = 0;
    while (matcher.find(pos))
    {
        count++;
        pos = matcher.start() + 1;
    }

    return count;
}

Example call:

Pattern pattern = Pattern.compile("232");
int count = countMatches(pattern, "23232"); // Returns 2

If you want a non-regular-expression search, just compile your pattern appropriately with the LITERAL flag:

Pattern pattern = Pattern.compile("1+1", Pattern.LITERAL);
int count = countMatches(pattern, "1+1+1"); // Returns 2
查看更多
萌妹纸的霸气范
7楼-- · 2018-12-31 06:54

Increment lastIndex whenever you look for next occurrence.

Otherwise it's always finding the first substring (at position 0).

查看更多
登录 后发表回答