0

If I have a string like follows:

"abc; expire=Thu, 16-Oct-2014 16:46:53 GMT; path=/;", and I am trying to extract everything between expire= and the next ; so I can get the timestamp, how would I do this in Swift? I am totally confused by the ideas of characters and StringIndex. I feel like the following code will give me the locations in the string of " expire=":

var range = cookie!.rangeOfString(" expires=");

I don't however understand where I should go from there.

In Java-like languages the algorithm would be something like:

1) Locate index of "expires=";

2) Take substring of everything after "expires=" to end of full string;

3) Look in substring for first occurrence of ";"

4) Everything from index 0 to index found in 3 is the expire timestamp.

How do you go about working with Swift strings?

2 Answers 2

3

In Swift you can use regex to search strings, without having to deal with NSRegularExpression explicitly:

let str = "abc; expire=Thu, 16-Oct-2014 16:46:53 GMT; path=/;"
let range = str.rangeOfString("(?<=expire=)[^;]*", options: .RegularExpressionSearch)
if let range = optRange? {
    print(str.substringWithRange(range))
} else {
    print("Not found")
}

The regular expression is straightforward: [^;]* means "a sequence of characters up to the first semicolon"; (?<=expire=) means "match only when preceded by expire= string".

Demo.

Sign up to request clarification or add additional context in comments.

2 Comments

I am not against using regular expressions if necessary, but shouldn't there be a couple basic string operations that would enable me to do this without them?
@thatidiotguy Absolutely, it is possible to rewrite this as a sequence of finding substrings, arriving at the same result after a sequence of simple subtractions. This would make a four-line solution out of a one-line solution, which is suboptimal from the point of view of the reader of the code.
1

Personally I'd go with @dasblinkenlight's solution of using a regular expression, but if you want to do something more like the algorithm in your question, you can:

let cookie: String = "abc; expires=Thu, 16-Oct-2014 16:46:53 GMT; path=/;"
if let range = cookie.rangeOfString(" expires=") {
    let expiresAndPath = cookie[range.endIndex..<cookie.endIndex]
    if let semicolonRange = expiresAndPath.rangeOfString(";") {
        let expires = expiresAndPath[expiresAndPath.startIndex..<semicolonRange.startIndex]
        /* do something with expires */
    }
}

Step-by-step:

  1. Locate index of "expires="

let range = cookie.rangeOfString(" expires=")

  1. Take substring of everything after "expires=" to end of full string

let expiresAndPath = cookie[range.endIndex..<cookie.endIndex]

  1. Look in substring for first occurrence of ";"

let semicolonRange = expiresAndPath.rangeOfString(";")

  1. Everything from index 0 to index found in 3 is the expire timestamp.

let expires = expiresAndPath[expiresAndPath.startIndex..<semicolonRange.startIndex]

1 Comment

I am accepting your answer as this follows the algorithm I spelled out in OP. Although I am finding this range stuff to be a little wild coming from a Java background, this definitely helped me understand this a lot better. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.