Occurrences of substring in a string

后端 未结 24 1755
眼角桃花
眼角桃花 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

    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= findStrLength){
                        if(str.substring(i, i+findStrLength).equals(findStr)){
                            count++;
                        }
                    }
                }
            }
            System.out.println(count);
    

提交回复
热议问题