Occurrences of substring in a string

后端 未结 24 1869
眼角桃花
眼角桃花 2020-11-22 03:35

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 = \"helloslkhe         


        
相关标签:
24条回答
  • 2020-11-22 03:45

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

        String searchPattern="aaa"; // search string
        String str="aaaaaababaaaaaa"; // whole string
        int searchLength = searchPattern.length(); 
        int totalLength = str.length(); 
        int k = 0;
        for (int i = 0; i < totalLength - searchLength + 1; i++) {
            String subStr = str.substring(i, searchLength + i);
            if (subStr.equals(searchPattern)) {
               k++;
            }
    
        }
    
    0 讨论(0)
  • 2020-11-22 03:45

    here is the other solution without using regexp/patterns/matchers or even not using StringUtils.

    String str = "helloslkhellodjladfjhelloarunkumarhelloasdhelloaruhelloasrhello";
            String findStr = "hello";
            int count =0;
            int findStrLength = findStr.length();
            for(int i=0;i<str.length();i++){
                if(findStr.startsWith(Character.toString(str.charAt(i)))){
                    if(str.substring(i).length() >= findStrLength){
                        if(str.substring(i, i+findStrLength).equals(findStr)){
                            count++;
                        }
                    }
                }
            }
            System.out.println(count);
    
    0 讨论(0)
  • 2020-11-22 03:46

    Here it is, wrapped up in a nice and reusable method:

    public static int count(String text, String find) {
            int index = 0, count = 0, length = find.length();
            while( (index = text.indexOf(find, index)) != -1 ) {                
                    index += length; count++;
            }
            return count;
    }
    
    0 讨论(0)
  • 2020-11-22 03:46

    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
    
    0 讨论(0)
  • 2020-11-22 03:51

    Based on the existing answer(s) I'd like to add a "shorter" version without the if:

    String str = "helloslkhellodjladfjhello";
    String findStr = "hello";
    
    int count = 0, lastIndex = 0;
    while((lastIndex = str.indexOf(findStr, lastIndex)) != -1) {
        lastIndex += findStr.length() - 1;
        count++;
    }
    
    System.out.println(count); // output: 3
    
    0 讨论(0)
  • 2020-11-22 03:52

    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;
    }
    
    0 讨论(0)
提交回复
热议问题