0

I am new to Swift 4 and practising swift. I am not able to figure out how to find a index of a particular substring inside a string. Below is code :

var hello = "Hello This is HelloWorld !"
if let idx = hello.characters.index(of: "!") {
    hello.insert(contentsOf: ", world".characters, at: idx)
}

I need a function which return me "HelloWorld" index from var hello. I think there should be inbuilt function but i couldnt find it. i Have seen this post of stack overflow Index of a substring in a string with Swift but most of code is outdated. please help me to sort this .

  • so there is no inbuild index of function in swift ? –  Jul 19 '17 at 10:11
  • it may be duplicate question but solution of post is not working swift 4 as its throwing error "advanceBy is unavailable" –  Jul 19 '17 at 10:17
  • 1
    It is just `if let range = string.range(of: substring) { ... }`, as you can see from the first answer. You don't have to use the custom extensions. That should work in Swift 3 and 4. – Martin R Jul 19 '17 at 11:08

0 Answers0