J. Scott
J. Scott

Reputation: 27

Slicing Strings Swift

I want to slice a very long string from one word to another. I want to get the substring between those words. For that, I use the following string extension:

 extension String {
 func slice(from: String, to: String) -> String? {

 guard let rangeFrom = range(of: from)?.upperBound else { return nil }

 guard let rangeTo = self[rangeFrom...].range(of: to)?.lowerBound else { return nil }

 return String(self[rangeFrom..<rangeTo])
 }

That works really good, but my raw-string contains a few of the "from" "to"-words and I need every substring that is between of these two words, but with my extension I can ony get the first substring.

Example:

let raw = "id:244476end36475677id:383848448end334566777788id:55678900end543"

I want to get the following substrings from this raw string example:

sub1 = "244476"
sub2 = "383848448"
sub3 = "55678900"

If I call:

var text = raw.slice(from: "id:" , to: "end")

I only get the first occurence (text = "244476")

Thank you for reading. Every answer would be nice.

PS: I get always an error by making code snippets in stackoverflow.

Upvotes: 1

Views: 794

Answers (2)

Leo Dabus
Leo Dabus

Reputation: 236360

You can get the ranges of your substrings using a while loop to repeat the search from that point to the end of your string and use map to get the substrings from the resulting ranges:

extension StringProtocol {
    func ranges<S:StringProtocol,T:StringProtocol>(between start: S, and end: T, options: String.CompareOptions = []) -> [Range<Index>] {
        var ranges: [Range<Index>] = []
        var startIndex = self.startIndex
        while startIndex < endIndex,
            let lower = self[startIndex...].range(of: start, options: options)?.upperBound,
            let range = self[lower...].range(of: end, options: options) {
            let upper = range.lowerBound
            ranges.append(lower..<upper)
            startIndex = range.upperBound
        }
        return ranges
    }
    func substrings<S:StringProtocol,T:StringProtocol>(between start: S, and end: T, options: String.CompareOptions = []) -> [SubSequence] {
        ranges(between: start, and: end, options: options).map{self[$0]}
    }
}

Playground testing:

let string = """
your text
id:244476end
id:383848448end
id:55678900end
the end
"""

let substrings = string.substrings(between: "id:", and: "end")  // ["244476", "383848448", "55678900"]

Upvotes: 1

Alexander
Alexander

Reputation: 63271

Rather thant trying to parse the string from start to end, I would use a combination of existing methods to transform it into the desire result. Here's How I would do this:

import Foundation

let raw = "id:244476end36475677id:383848448end334566777788id:55678900end543"

let result = raw
    .components(separatedBy: "id:")
    .filter{ !$0.isEmpty }
    .map { segment -> String in
        let slices = segment.components(separatedBy: "end")
        return slices.first! // Removes the `end` and everything thereafter
    }

print(result) // => ["244476", "383848448", "55678900"]

Upvotes: 1

Related Questions