简体   繁体   English

Swift中的字符串操作(子字符串)

[英]String Operations (substring) in Swift

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 ; “ abc; expire = Thu,16-Oct-2014 16:46:53 GMT; path = /;”,我正在尝试提取expire=和next之间的所有内容; so I can get the timestamp, how would I do this in Swift? 所以我可以获得时间戳,我该如何在Swift中做到这一点? I am totally confused by the ideas of characters and StringIndex. 我对字符和StringIndex的想法完全感到困惑。 I feel like the following code will give me the locations in the string of " expire=" : 我觉得以下代码将给我字符串" 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: 在类似Java的语言中,算法将类似于:

1) Locate index of "expires="; 1)找到索引“ expires =“;

2) Take substring of everything after "expires=" to end of full string; 2)将“ expires =“之后的所有内容的子字符串作为完整字符串的结尾;

3) Look in substring for first occurrence of ";" 3)在子字符串中查找“;”的首次出现

4) Everything from index 0 to index found in 3 is the expire timestamp. 4)从索引0到3中找到的所有索引都是到期时间戳。

How do you go about working with Swift strings? 您如何使用Swift字符串?

In Swift you can use regex to search strings, without having to deal with NSRegularExpression explicitly: 在Swift中,您可以使用正则表达式搜索字符串,而不必显式处理NSRegularExpression

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". (?<=expire=)意思是“仅在以expire=字符串expire=时匹配”。

Demo. 演示。

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: 就我个人而言,我会使用@dasblinkenlight使用正则表达式的解决方案,但是如果您想做更多类似于问题中的算法的操作,则可以:

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=" 找到索引“ expires =“

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

  1. Take substring of everything after "expires=" to end of full string 将“ expires =“之后的所有内容的子字符串作为完整字符串的结尾

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. 从索引0到索引3中的所有内容都是过期时间戳记。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM