Occurrences of substring in a string

后端 未结 24 2345
眼角桃花
眼角桃花 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 04:03

    The answer given as correct is no good for counting things like line returns and is far too verbose. Later answers are better but all can be achieved simply with

    str.split(findStr).length
    

    It does not drop trailing matches using the example in the question.

提交回复
热议问题