Counting the occurrences of a substring in string in Swift

Counting the occurrences of a substring in string in Swift

In this post we will give you information about Counting the occurrences of a substring in string in Swift. Hear we will give you detail about Counting the occurrences of a substring in string in SwiftAnd how to use it also give you demo for it if it is necessary.

Learn, how to count the total number of occurrences of a substring in a string with swift.

Counting the occurrences

We can count the number of occurrences of a substring in the string using the components(separatedBy:) instance method in Swift.

Here is an example, that counts the number of times substring hi is present in the following string:

import Foundationlet msg = "hi, Welcome to party, hi Swift hi"let result =  msg.components(separatedBy:"hi")print(result.count-1)

Output:

3

Hope this code and post will helped you for implement Counting the occurrences of a substring in string in Swift. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US