swift post请求是否支持range

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.
I want to learn the best/simplest way to turn a string into another string but with only a subset, starting at the beginning and going to the last index of a character.
For example, convert "" to "www.stackoverflow".
What code snippet would do that, and being the most swift-like? (I hope this doesn't bring a debate, but I can't find good lesson on how to handle substrings in Swift.
1,26841332
Just accessing backwards
The best way is to use substringToIndex combined to the endIndexproperty and the advance global function.
var string1 = ""
var index1 = advance(string1.endIndex, -4)
var substring1 = string1.substringToIndex(index1)
Looking for a string starting from the back
Use rangeOfString and set options to .BackwardsSearch
var string2 = ""
var index2 = string2.rangeOfString(".", options: .BackwardsSearch)?.startIndex
var substring2 = string2.substringToIndex(index2!)
No extensions, pure idiomatic Swift
Here's how I do it. You could do it the same way, or use this code for ideas.
let s = ""
s.substringWithRange(0..&s.lastIndexOf("."))
Here are the extensions I use:
import Foundation
extension String {
var length: Int {
return countElements(self)
func indexOf(target: String) -& Int {
var range = self.rangeOfString(target)
if let range = range {
return distance(self.startIndex, range.startIndex)
func indexOf(target: String, startIndex: Int) -& Int {
var startRange = advance(self.startIndex, startIndex)
var range = self.rangeOfString(target, options: NSStringCompareOptions.LiteralSearch, range: Range&String.Index&(start: startRange, end: self.endIndex))
if let range = range {
return distance(self.startIndex, range.startIndex)
func lastIndexOf(target: String) -& Int {
var index = -1
var stepIndex = self.indexOf(target)
while stepIndex & -1 {
index = stepIndex
if stepIndex + target.length & self.length {
stepIndex = indexOf(target, startIndex: stepIndex + target.length)
stepIndex = -1
return index
func substringWithRange(range:Range&Int&) -& String {
let start = advance(self.startIndex, range.startIndex)
let end = advance(self.startIndex, range.endIndex)
return self.substringWithRange(start..&end)
Generally I am a strong proponent of extensions, especially for needs like string manipulation, searching, and slicing.
For example, convert "" to "www.stackoverflow". What code snippet would do that, and being the most swift-like?
Can it be more Swift-like?
let myString = "".stringByDeletingPathExtension
// "www.stackoverflow"
20.1k32741
String has builtin substring feature:
extension String : Sliceable {
subscript (subRange: Range&String.Index&) -& String { get }
If what you want is "going to the first index of a character", you can get the substring using builtin find() function:
var str = ""
str[str.startIndex ..& find(str, ".")!] // -& "www"
To find last index, we can implement findLast().
/// Returns the last index where `value` appears in `domain` or `nil` if
/// `value` is not found.
/// Complexity: O(\ `countElements(domain)`\ )
func findLast&C: CollectionType where C.Generator.Element: Equatable&(domain: C, value: C.Generator.Element) -& C.Index? {
var last:C.Index? = nil
for i in domain.startIndex..&domain.endIndex {
if domain[i] == value {
return last
let str = ""
let substring = map(findLast(str, ".")) { str[str.startIndex ..& $0] } // as String?
// if "." is found, substring has some, otherwise `nil`
Maybe, BidirectionalIndexType specialized version of findLast is faster:
func findLast&C: CollectionType where C.Generator.Element: Equatable, C.Index: BidirectionalIndexType&(domain: C, value: C.Generator.Element) -& C.Index? {
for i in lazy(domain.startIndex ..& domain.endIndex).reverse() {
if domain[i] == value {
return nil
18.2k32151
You can use this extensions:
extension String
func substringFromIndex(index: Int) -& String
if (index & 0 || index & self.characters.count)
print("index \(index) out of bounds")
return self.substringFromIndex(self.startIndex.advancedBy(index))
func substringToIndex(index: Int) -& String
if (index & 0 || index & self.characters.count)
print("index \(index) out of bounds")
return self.substringToIndex(self.startIndex.advancedBy(index))
func substringWithRange(start: Int, end: Int) -& String
if (start & 0 || start & self.characters.count)
print("start index \(start) out of bounds")
else if end & 0 || end & self.characters.count
print("end index \(end) out of bounds")
let range = Range(start: self.startIndex.advancedBy(start), end: self.startIndex.advancedBy(end))
return self.substringWithRange(range)
func substringWithRange(start: Int, location: Int) -& String
if (start & 0 || start & self.characters.count)
print("start index \(start) out of bounds")
else if location & 0 || start + location & self.characters.count
print("end index \(start + location) out of bounds")
let range = Range(start: self.startIndex.advancedBy(start), end: self.startIndex.advancedBy(start + location))
return self.substringWithRange(range)
let string = ""
let substring = string.substringToIndex(string.characters.count-4)
For Swift 2.0, it's like this:
var string1 = ""
var index1 = string1.endIndex.advanceBy(-4)
var substring1 = string1.substringToIndex(index1)
1,80011327
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled360浏览器的兼容模式不能$.post???
[问题点数:40分,结帖人a2523557]
360浏览器的兼容模式不能$.post???
[问题点数:40分,结帖人a2523557]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
相关推荐:
2013年5月 总版技术专家分月排行榜第一
2014年8月 总版技术专家分月排行榜第二2014年7月 总版技术专家分月排行榜第二2013年6月 总版技术专家分月排行榜第二
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 swift range转nsrange 的文章

 

随机推荐