我还没有弄清楚如何在Swift中获得字符串的子字符串:

var str = “Hello, playground”
func test(str: String) -> String {
 return str.substringWithRange( /* What goes here? */ )
}
test (str)

我不能在Swift中创建一个范围。自动完成在游乐场不是超级有用的-这是它的建议:

return str.substringWithRange(aRange: Range<String.Index>)

我在Swift标准参考库中没有找到任何有用的东西。下面是另一个大胆的猜测:

return str.substringWithRange(Range(0, 1))

这:

let r:Range<String.Index> = Range<String.Index>(start: 0, end: 2)
return str.substringWithRange(r)

我看到了其他答案(在Swift字符串中查找字符索引),似乎表明,由于字符串是NSString的桥类型,“旧”方法应该工作,但不清楚如何-例如,这也不工作(似乎不是有效的语法):

let x = str.substringWithRange(NSMakeRange(0, 3))

想法吗?


当前回答

在操场上试试这个

var str:String = "Hello, playground"

let range = Range(start:advance(str.startIndex,1), end: advance(str.startIndex,8))

它会给出"ello, p"

然而,有趣的是,如果你使最后一个索引大于playground中的字符串,它将显示你在str:o之后定义的任何字符串

Range()似乎是一个泛型函数,因此它需要知道它正在处理的类型。

你还必须给它实际的字符串,你感兴趣的游乐场,因为它似乎持有所有的刺在一个接一个的序列与他们的变量名之后。

So

var str:String = "Hello, playground"

var str2:String = "I'm the next string"

let range = Range(start:advance(str.startIndex,1), end: advance(str.startIndex,49))

我是下一条线

即使str2使用let定义也有效

:)

其他回答

let startIndex = text.startIndex
var range = startIndex.advancedBy(1) ..< text.endIndex.advancedBy(-4)
let substring = text.substringWithRange(range)

你可以在这里看到完整的样本

您可以使用这些扩展来改进substringWithRange

斯威夫特2.3

extension String
{   
    func substringWithRange(start: Int, end: Int) -> String
    {
        if (start < 0 || start > self.characters.count)
        {
            print("start index \(start) out of bounds")
            return ""
        }
        else if end < 0 || end > self.characters.count
        {
            print("end index \(end) out of bounds")
            return ""
        }
        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")
            return ""
        }
        else if location < 0 || start + location > self.characters.count
        {
            print("end index \(start + location) out of bounds")
            return ""
        }
        let range = Range(start: self.startIndex.advancedBy(start), end: self.startIndex.advancedBy(start + location))
        return self.substringWithRange(range)
    }
}

斯威夫特3

extension String
{
    func substring(start: Int, end: Int) -> String
    {
        if (start < 0 || start > self.characters.count)
        {
            print("start index \(start) out of bounds")
            return ""
        }
        else if end < 0 || end > self.characters.count
        {
            print("end index \(end) out of bounds")
            return ""
        }
        let startIndex = self.characters.index(self.startIndex, offsetBy: start)
        let endIndex = self.characters.index(self.startIndex, offsetBy: end)
        let range = startIndex..<endIndex

        return self.substring(with: range)
    }

    func substring(start: Int, location: Int) -> String
    {
        if (start < 0 || start > self.characters.count)
        {
            print("start index \(start) out of bounds")
            return ""
        }
        else if location < 0 || start + location > self.characters.count
        {
            print("end index \(start + location) out of bounds")
            return ""
        }
        let startIndex = self.characters.index(self.startIndex, offsetBy: start)
        let endIndex = self.characters.index(self.startIndex, offsetBy: start + location)
        let range = startIndex..<endIndex

        return self.substring(with: range)
    }
}

用法:

let str = "Hello, playground"

let substring1 = str.substringWithRange(0, end: 5) //Hello
let substring2 = str.substringWithRange(7, location: 10) //playground

斯威夫特3。X+ Xcode 8。X +测试工作轻松解决方案;

使用简单;

let myString = "12.12.2017 12:34:45"
let newString = myString?[(myString?.startIndex)!..<(myString?.index((myString?.startIndex)!, offsetBy: 10))!]
print(newString) 

输出= 12.12.2017

便于定制更改;

offsetBy: 10 // Change 10 to endIndex.

当你改变偏移量:10到15,20等。会割断你的绳子。

谢谢你!

由于String是NSString的桥接类型,“旧的”方法应该可以工作,但不清楚如何工作-例如,这也不起作用(似乎不是有效的语法): let x = str.substringWithRange(NSMakeRange(0,3))

对我来说,这是你问题中真正有趣的部分。String被桥接到NSString,所以大多数NSString方法直接作用于String。你可以自由地使用它们,不需要思考。例如,这正如你所期望的那样:

// delete all spaces from Swift String stateName
stateName = stateName.stringByReplacingOccurrencesOfString(" ", withString:"")

But, as so often happens, "I got my mojo workin' but it just don't work on you." You just happened to pick one of the rare cases where a parallel identically named Swift method exists, and in a case like that, the Swift method overshadows the Objective-C method. Thus, when you say str.substringWithRange, Swift thinks you mean the Swift method rather than the NSString method — and then you are hosed, because the Swift method expects a Range<String.Index>, and you don't know how to make one of those.

最简单的办法就是阻止霉霉这样做,明确地说:

let x = (str as NSString).substringWithRange(NSMakeRange(0, 3))

注意,这里没有涉及到重大的额外工作。“转换”并不意味着“转换”;String实际上是一个NSString。我们只是告诉Swift为了这一行代码的目的如何看待这个变量。

整个事情中真正奇怪的部分是导致所有这些麻烦的Swift方法是没有记录的。我不知道它的定义是什么;它不在NSString头文件中也不在Swift头文件中。

好吧,我有同样的问题,并解决了“bridgetooobjecvec()”函数:

var helloworld = "Hello World!"
var world = helloworld.bridgeToObjectiveC().substringWithRange(NSMakeRange(6,6))
println("\(world)") // should print World!

请注意,在这个例子中,substringWithRange与NSMakeRange结合使用了字符串的一部分,从索引6开始(字符“W”),并在索引6 + 6位前结束(字符“!”)

欢呼。