You should leave one side empty, hence the name "partial range".

let newStr = str[..<index]

The same stands for partial range from operators, just leave the other side empty:

let newStr = str[index...]

Keep in mind that these range operators return a Substring. If you want to convert it to a string, use String's initialization function:

let newStr = String(str[..<index])

You can read more about the new substrings here.

Answer from user3151675 on Stack Overflow
🌐
Swift by Sundell
swiftbysundell.com › articles › slicing-swift-collections
Slicing Swift collections | Swift by Sundell
However, by doing so we’re following the same conventions as the standard library does — enabling the call site to decide how and when to convert each slice, which in turn enables us to perform additional chaining without performance penalties. Finally, let’s take a look at a third variant of collection slicing — splitting, which is a technique that’s incredibly commonly used when working with strings. Swift offers two main ways of splitting strings — the String type’s own split method, as well as Foundation’s components(separatedBy:) API (which was inherited from Objective-C’s NSString):
🌐
Swift Forums
forums.swift.org › evolution › discussion
String slicing ergonomics - Discussion - Swift Forums
April 27, 2018 - After years of using Swift I continue to have to search how to slice a string each time. It's extremely cumbersome and user-hostile · In another thread (on phone so hard to look up) there is a proposal to add subscript with an argument named o (for offset) to RandomAcessCollection which would ...
🌐
Cocoaphile
cocoaphile.com › uncategorized › string slicing in swift
String Slicing in Swift | Cocoaphile
😁" let substr = str[0] // error: 'subscript' is unavailable: cannot subscript String with an Int let substr = str[0..<5] // error: 'subscript' is unavailable: cannot subscript String with a CountableRange<Int> Swift Strings use a non-integer index type for reasons of speed and safety:
🌐
DEV Community
dev.to › bibinjaimon › how-to-slice-a-substring-from-swift-string-8m2
How to slice a substring from Swift string - DEV Community
April 4, 2021 - extension StringProtocol { func slice(_ start: Int, _ end: Int) -> SubSequence { let lower = index(self.startIndex, offsetBy: start) let upper = index(lower, offsetBy: end - start) return self[lower...upper] } } "BibinJ".slice(2, 4) // Answer is "bin" ... #ios #swift #softwareengineering #developer #4 SOLID - Interface Segregation Principle | Swift | iOS Development
🌐
Swift Forums
forums.swift.org › using swift
Dealing with String and Array slices - Using Swift - Swift Forums
June 13, 2022 - From what I can see, you cannot simply pass a array or string slice to a function that expects an array or string: func sayHi(name: String) { print("Hello \(name)") } You can't simply do: let sentence = "My name…
🌐
Medium
akarshseggemu.medium.com › string-slicing-substring-mastering-swift-4-fourth-edition-d931f09a0ccb
Swift: String slicing substring — Mastering Swift 4 — Fourth Edition - Akarsh Seggemu, M.Sc. - Medium
July 23, 2022 - Code deprecation error in playground when I was trying the example mentioned in the page 79 of the book “Mastering Swift 4 — Fourth Edition”. ... path.substring(to: startIndex) // returns the “/one”path.substring(from: endIndex) // returns the “/four” ... you get error in playground to change the code and use String slicing subscript with a 'partial range from' operator.
Find elsewhere
🌐
Use Your Loaf
useyourloaf.com › blog › updating-strings-for-swift-4
Updating Strings For Swift 4
January 16, 2020 - If you want to store it or pass it around convert it back to a String. In Swift 4 you slice a string into a substring using subscripting.
🌐
Swiftunboxed
swiftunboxed.com › stdlib › substrings
Swift Substrings - Swift Unboxed
_StringCore has many more complexities, but this quick look at properties should get us most of the way there: strings have some underlying storage and size. How do you construct a substring in Swift? The easiest way is to take a slice of a string via subscript:
🌐
Dot Net Perls
dotnetperls.com › substring-swift
Swift - String Substring Examples - Dot Net Perls
let value = "lion" // A string cannot be accessed with Ints. var char = value[ ... In Swift 4 and beyond we must return a String with a special call.
Top answer
1 of 2
1

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"]
2 of 2
1

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"]
🌐
GitHub
github.com › apple › swift › blob › main › stdlib › public › core › ArraySlice.swift
swift/stdlib/public/core/ArraySlice.swift at main · swiftlang/swift
/// This example demonstrates getting a slice of an array of strings, finding · /// the index of one of the strings in the slice, and then using that index · /// in the original array.
Author   swiftlang
🌐
Waldo
waldo.com › blog › swift-split-string
How to Split a String in Swift: 4 Useful Examples | Waldo Blog
April 4, 2022 - You can get a substring from a string using the removeSubrange() function. In the example below, we have a string with the name strH, which is “Hello Swift Programmers.” Now, we want to extract “Swift Programmers” from it.
🌐
GitHub
github.com › scinfu › SwiftSoup › issues › 32
'substring(with:)' is deprecated: Please use String slicing subscript. (Swift 4) · Issue #32 · scinfu/SwiftSoup
June 19, 2017 - static func split(_ value: String, _ offset: Int, _ count: Int) -> String { let start = value.index(value.startIndex, offsetBy: offset) let end = value.index(value.startIndex, offsetBy: count+offset) let range = start..<end //return value.substring(with: range) return String(value[range]) }
Published   Sep 22, 2017