How to properly use indexes and ranges in Swift to isolate substrings | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How to properly use indexes and ranges in Swift to isolate substrings

I want to find the last occurance of either of two characters in a string and then create a substring with all of the characters that follow. For example given the string: This is my + string with +GetThisSubstring I want the substring to be 'GetThisSubstring' Or if the string is: This is my / string with /GetThisSubstring I want the substring to also be 'GetThisSubstring' I tried using indexes and ranges, but just could not seem to get it right. I have done a lot of searching and reading (not here on Sololearn as there is very little Swift Q&A content). I ended up using a different approach (see below) but would like to understand how to use indexes and ranges to access substring of strings. let theText = readLine()! var textPart = "" for c in theText.reversed() { if c == "+" || c == "/" {break} textPart += String(c) } print(String(textPart.reversed())) // GetThisSubstring

23rd Apr 2020, 6:22 AM
Paul K Sadler
Paul K Sadler - avatar
8 Answers
+ 3
Check the link to the documentation for available string methods and their usage. You can use the contains method to check if a character exists in a string. Yes you can set a character to a variable and pass it to split method instead of using a literal. There is also a indexof() method that could be used to get the first index of that character. You can loop over the string and compare each char to the char you're looking for. And save the indices in an array or collection. var indices [int] for i in str.indices { ...code to check, add index to array, etc } You can also use ranges to slice the string and get a substring of that string [start_index...end_index] / inclusive [start..<end] / start inclusive, end exclusive [...end] / open start (from beginning) to end inclusive etc There are lots of way to accomplish what you're after.
23rd Apr 2020, 5:47 PM
ChaoticDawg
ChaoticDawg - avatar
+ 5
ChaoticDawg that makes sense. I really appreciate you taking the time to explain it.
25th Apr 2020, 6:58 PM
Paul K Sadler
Paul K Sadler - avatar
+ 4
ChaoticDawg so does that mean the customers[0], customers[1], etc are substrings of the string customers. Or are they substrings of the console buffer?
25th Apr 2020, 6:43 PM
Paul K Sadler
Paul K Sadler - avatar
+ 3
ChaoticDawg so I don't understand why this doesn't work... let name = readLine()! + "z" var customers = (readLine()! + " " + name).split(separator: " ") customers.sort() if let index = customers.firstIndex(of: name) { print(index) } But if I replace name with a string literal it works. if let index = customers.firstIndex(of: "Ben") { print(index) }
25th Apr 2020, 5:41 PM
Paul K Sadler
Paul K Sadler - avatar
+ 2
It seems that what you really want is to split the string into substrings based on a given char. You should just use the split method which will return an array of substrings. https://developer.apple.com/documentation/swift/string/2894564-split You can then get the substrings using their indices let subs = "This is my / string with /GetThisSubstring" let subsarr = subs.split(seperator: "/") // ["This is my ", " string with ", "GetThisSubstring"] let sub = subsarr[2] // "GetThisSubstring"]
23rd Apr 2020, 7:14 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ChaoticDawg thank you, I like that approach too. I could use count to check if there was a given seperator. I wonder if the separator can be supplied as a variable. I would need to do two passes to account for two possible separators. I would still like to figure out how to use index and range. I couldn't figure out how to find the last index of + and then set a range to its location plus one to the end of the string so I could access it as string[range] 🙃
23rd Apr 2020, 4:23 PM
Paul K Sadler
Paul K Sadler - avatar
+ 2
It doesn't work because they are different types. name is inferred as type String customers is an array of type Substring "Ben" is being inferred as type Substring due to it being the expected type and being able to be passed to the Substring(String) constructor implicitly. All you need to do here is explicitly convert name to type Substring using its constructor Substring(name) .firstIndex(of: Substring(name))
25th Apr 2020, 6:24 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Neither, they are substrings of the string returned from (readLine()! + " " + name). It will create a new string object in memory and its reference is used for the split() method so it is held in memory and used for the customers Substring array.
25th Apr 2020, 6:56 PM
ChaoticDawg
ChaoticDawg - avatar