0

I'm new to swift.

I have a requirement to get substring from a string and till end index of string

Eg:

My string is Hello Playground how are you?

If my substring contains "how", it should return me "how are you?"

If my substring contains "Hello". it should return "Hello Playground how are you?"

Similary if my substring contains "playground", it should return "Playground how are you?"

I can use contains method to see if substring is present or not

If yes, then how can i get the index of that substring is puzzling me

If I can get index of my substring i may use something like

let mySubstringString = str[substring.startIndex..

Please advice

Ashh
  • 569
  • 1
  • 6
  • 28
  • Possible duplicate of [How does String substring work in Swift](https://stackoverflow.com/questions/39677330/how-does-string-substring-work-in-swift) – Tomte Jan 28 '19 at 07:25

1 Answers1

0

You can try this,

let string = "Hello Playground how are you?"
let sub = "Playground"
if let startIndex = string.range(of: sub)?.lowerBound {
    let newString = string[startIndex..<string.endIndex]
    print(newString) // Playground how are you?
}
Kamran
  • 14,987
  • 4
  • 33
  • 51