How to count the occurrences of a substring in string with Swift

1 Answer

0 votes
import Foundation

let s = "swift java c c++ swiftswift php c# swift"

let result =  s.components(separatedBy:"swift")

print(result.count - 1)




/*
run:

4

*/

 



answered Feb 22, 2021 by avibootz
...