Greetings!
So I wrote this function and then felt like sharing it with you guys.
Which basically helps you retrieve a Substring from an string.
Here is the function.
1 2 3 4 5 6 7 8 9 10 |
func getSubstring(mainString: String, startString: String,endString: String) -> String { var tempString = mainString.lowercaseString let start = tempString.rangeOfString(startString) let range = Range(start: tempString.startIndex, end: (start?.endIndex)!) tempString.removeRange(range) let substring = tempString.substringWithRange(Range(start: tempString.startIndex,end: (tempString.rangeOfString(endString)?.startIndex)!)) return substring } |
How it works?
You need to pass your whole string along with the string at -1th index of the starting index of your substring and the string at +1th index of the ending index of your substring. In return you’ll get your substring.
Check the result below:
Ciao!