Index of a substring in a string with Swift

前端 未结 11 1701
自闭症患者
自闭症患者 2020-11-22 14:24

I\'m used to do this in JavaScript:

var domains = \"abcde\".substring(0, \"abcde\".indexOf(\"cd\")) // Returns \"ab\"

Swift doesn\'t have t

11条回答
  •  长情又很酷
    2020-11-22 15:10

    There are three closely connected issues here:

    • All the substring-finding methods are over in the Cocoa NSString world (Foundation)

    • Foundation NSRange has a mismatch with Swift Range; the former uses start and length, the latter uses endpoints

    • In general, Swift characters are indexed using String.Index, not Int, but Foundation characters are indexed using Int, and there is no simple direct translation between them (because Foundation and Swift have different ideas of what constitutes a character)

    Given all that, let's think about how to write:

    func substring(of s: String, from:Int, toSubstring s2 : String) -> Substring? {
        // ?
    }
    

    The substring s2 must be sought in s using a String Foundation method. The resulting range comes back to us, not as an NSRange (even though this is a Foundation method), but as a Range of String.Index (wrapped in an Optional, in case we didn't find the substring at all). However, the other number, from, is an Int. Thus we cannot form any kind of range involving them both.

    But we don't have to! All we have to do is slice off the end of our original string using a method that takes a String.Index, and slice off the start of our original string using a method that takes an Int. Fortunately, such methods exist! Like this:

    func substring(of s: String, from:Int, toSubstring s2 : String) -> Substring? {
        guard let r = s.range(of:s2) else {return nil}
        var s = s.prefix(upTo:r.lowerBound)
        s = s.dropFirst(from)
        return s
    }
    

    Or, if you prefer to be able to apply this method directly to a string, like this...

    let output = "abcde".substring(from:0, toSubstring:"cd")
    

    ...then make it an extension on String:

    extension String {
        func substring(from:Int, toSubstring s2 : String) -> Substring? {
            guard let r = self.range(of:s2) else {return nil}
            var s = self.prefix(upTo:r.lowerBound)
            s = s.dropFirst(from)
            return s
        }
    }
    

提交回复
热议问题