我还没有弄清楚如何在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))

想法吗?


当前回答

如果你有一个NSRange,桥接到NSString无缝工作。例如,我正在用UITextFieldDelegate做一些工作,当它询问是否应该替换范围时,我很快想要计算新的字符串值。

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
    println("Got new string: ", newString)
}

其他回答

借鉴Rob Napier的经验,我开发了这些常见的字符串扩展,其中两个是:

subscript (r: Range<Int>) -> String
{
    get {
        let startIndex = advance(self.startIndex, r.startIndex)
        let endIndex = advance(self.startIndex, r.endIndex - 1)

        return self[Range(start: startIndex, end: endIndex)]
    }
}

func subString(startIndex: Int, length: Int) -> String
{
    var start = advance(self.startIndex, startIndex)
    var end = advance(self.startIndex, startIndex + length)
    return self.substringWithRange(Range<String.Index>(start: start, end: end))
}

用法:

"Awesome"[3...7] //"some"
"Awesome".subString(3, length: 4) //"some"

由于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头文件中。

如何在Swift 2.0中获取子字符串的示例代码

(i)起始索引的子字符串

输入:-

var str = "Swift is very powerful language!"
print(str)

str = str.substringToIndex(str.startIndex.advancedBy(5))
print(str)

输出:

Swift is very powerful language!
Swift

(ii)特定索引的子字符串

输入:-

var str = "Swift is very powerful language!"
print(str)

str = str.substringFromIndex(str.startIndex.advancedBy(6)).substringToIndex(str.startIndex.advancedBy(2))
print(str)

输出:

Swift is very powerful language!
is

希望对你有所帮助!

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

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

您可以使用substringWithRange方法。它有一个开头和结尾String.Index。

var str = "Hello, playground"
str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex)) //"Hello, playground"

要更改开始和结束索引,请使用advancedBy(n)。

var str = "Hello, playground"
str.substringWithRange(Range<String.Index>(start: str.startIndex.advancedBy(2), end: str.endIndex.advancedBy(-1))) //"llo, playgroun"

你也可以用NSRange使用NSString方法,但你必须确保你使用的是这样的NSString:

let myNSString = str as NSString
myNSString.substringWithRange(NSRange(location: 0, length: 3))

注意:正如JanX2提到的,第二个方法对于unicode字符串是不安全的。