Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get substring after last occurrence of character in string: Swift IOS

Tags:

string

swift

Basically, I want to make a program where if you start typing a name, the app will recognize it from the database and fill in the name for you. To do this, if a person types a comma after finishing a name, the app will start recording what the next name is. If it matches as a substring of one of the names in the database, it will fill it in for the user. The issue is that I need to get what part of the name has been filled out so far after the last occurrence of the comma character in the textField string, but I don't know how. For example: User types: "Daniel, Joh" And the app fills in John for you. Thanks.

like image 606
dhruvm Avatar asked Apr 28 '16 04:04

dhruvm


2 Answers

If you really want the characters after the last comma, you could use a regular expression:

let string = "Hamilton, A"
let regex = try! NSRegularExpression(pattern: ",\\s*(\\S[^,]*)$")
if let match = regex.firstMatch(in: string, range: string.nsRange), let result = string[match.range(at: 1)] {
    // use `result` here
}

Where, in Swift 4:

extension String {

    /// An `NSRange` that represents the full range of the string.

    var nsRange: NSRange {
        return NSRange(startIndex ..< endIndex, in: self)
    }

    /// Substring from `NSRange`
    ///
    /// - Parameter nsRange: `NSRange` within the string.
    /// - Returns: `Substring` with the given `NSRange`, or `nil` if the range can't be converted.

    subscript(nsRange: NSRange) -> Substring? {
        return Range(nsRange, in: self)
            .flatMap { self[$0] }
    }
}
like image 158
Rob Avatar answered Oct 25 '22 08:10

Rob


Many thanks to Rob. We can even extend his String extension by including the full answer to the initial question, with any Character:

extension String {

func substringAfterLastOccurenceOf(_ char: Character) -> String {

    let regex = try! NSRegularExpression(pattern: "\(char)\\s*(\\S[^\(char)]*)$")
    if let match = regex.firstMatch(in: self, range: self.nsRange), let result = self[match.range(at: 1)] {
        return String(result)
    }
    return ""
}

// ... Rob's String extension 

}

So we just need to call:

let subStringAfterLastComma = "Hamilton, A".substringAfterLastOccurenceOf(",")
like image 28
Thierry G. Avatar answered Oct 25 '22 06:10

Thierry G.