I would like to get the start and end position of a substring within a string. Example: in the string "hi this is my name"; if I provide the string "this" I would like to know the start index is 4 and end index is 7.
I found several links, including this one:
Swift: get index of start of a substring in a string a-substring-in-a-string
But it doesn´t work in swift 3 as the method is now called range.
I am now using this:
let range = mystring.range(of: "StringSearch")?.lowerBound
which returns this
Swift.String.UnicodeScalarView.Index(_position: 15), _countUTF16: 1))
And I cannot obtain the position as an integer as this is an index.
In summary, I would like to have in a variable of type int the position, in this case 15.
Can anyone help me?
Thanks everyone.