Occurrences of substring in a string

后端 未结 24 1888
眼角桃花
眼角桃花 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条回答
  •  -上瘾入骨i
    2020-11-22 03:59

    How about using StringUtils.countMatches from Apache Commons Lang?

    String str = "helloslkhellodjladfjhello";
    String findStr = "hello";
    
    System.out.println(StringUtils.countMatches(str, findStr));
    

    That outputs:

    3
    

提交回复
热议问题