alp alp - 8 days ago 6
Swift Question

Swift Get string between 2 strings in a string

I am getting a string from html parse that is;

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


my code is something like

var startIndex = text.rangeOfString("'")
var endIndex = text.rangeOfString("',")
var range2 = startIndex2...endIndex
substr= string.substringWithRange(range)


i am not sure if my second splitting string should be "'" or "',"

i want my outcome as

substr = "Info/99/something"

seb seb
Answer

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

Swift 2.0: (XCode 7 Beta 6)

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
Comments