split a string by periods in swift

To split a string by periods in swift, you can use the split method of the String class. You can pass a Character as the separator to the method. In this case, the period . will be used as the separator.

Here's the code:

let str = "Hello.world"
let components = str.split(separator: ".")

// Output: ["Hello", "world"]
116 chars
6 lines

In the above code, the str variable contains the input string "Hello.world". The split method is called on this string with . as the separator. The resulting array of substrings is saved to the components constant. Finally, the contents of components are printed to the console. Since the string was split by periods, the output will be ["Hello", "world"].

gistlibby LogSnag