Number of occurrences of substring in string in Swift

后端 未结 11 949
夕颜
夕颜 2020-12-02 19:37

My main string is \"hello Swift Swift and Swift\" and substring is Swift. I need to get the number of times the substring \"Swift\" occurs in the mentioned string.

T

11条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 20:36

    Try this

    var mainString = "hello Swift Swift and Swift"
    var count = 0
    
    mainString.enumerateSubstrings(in: mainString.startIndex..

提交回复
热议问题