且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swift在字符串中的2个字符串之间获取字符串

更新时间:2023-02-04 23:01:44

我'使用正则表达式从这样的复杂输入中提取子串。

I'd use a regular expression to extract substrings from complex input like this.

Swift 3.1:

Swift 3.1:

let test = "javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);"

if let match = test.range(of: "(?<=')[^']+", options: .regularExpression) {
    print(test.substring(with: match))
}

// Prints: Info/99/something

Swift 2.0:

Swift 2.0:

let test = "javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);"

if let match = test.rangeOfString("(?<=')[^']+", options: .RegularExpressionSearch) {
    print(test.substringWithRange(match))
}

// Prints: Info/99/something