我怎样才能得到字符串的第n个字符?我尝试了括号([])访问器,没有运气。
var string = "Hello, world!"
var firstChar = string[0] // Throws error
错误:'下标'是不可用的:不能下标String与Int,请参阅文档注释讨论
我怎样才能得到字符串的第n个字符?我尝试了括号([])访问器,没有运气。
var string = "Hello, world!"
var firstChar = string[0] // Throws error
错误:'下标'是不可用的:不能下标String与Int,请参阅文档注释讨论
当前回答
我刚想出了一个巧妙的变通办法
var firstChar = Array(string)[0]
其他回答
为了提供主题并显示快速下标的可能性,这里有一个基于“substring-toolbox”下标的小字符串
这些方法是安全的,并且不会遍历字符串索引
extension String {
// string[i] -> one string char
subscript(pos: Int) -> String { return String(Array(self)[min(self.length-1,max(0,pos))]) }
// string[pos,len] -> substring from pos for len chars on the left
subscript(pos: Int, len: Int) -> String { return self[pos, len, .pos_len, .left2right] }
// string[pos, len, .right2left] -> substring from pos for len chars on the right
subscript(pos: Int, len: Int, way: Way) -> String { return self[pos, len, .pos_len, way] }
// string[range] -> substring form start pos on the left to end pos on the right
subscript(range: Range<Int>) -> String { return self[range.startIndex, range.endIndex, .start_end, .left2right] }
// string[range, .right2left] -> substring start pos on the right to end pos on the left
subscript(range: Range<Int>, way: Way) -> String { return self[range.startIndex, range.endIndex, .start_end, way] }
var length: Int { return countElements(self) }
enum Mode { case pos_len, start_end }
enum Way { case left2right, right2left }
subscript(var val1: Int, var val2: Int, mode: Mode, way: Way) -> String {
if mode == .start_end {
if val1 > val2 { let val=val1 ; val1=val2 ; val2=val }
val2 = val2-val1
}
if way == .left2right {
val1 = min(self.length-1, max(0,val1))
val2 = min(self.length-val1, max(1,val2))
} else {
let val1_ = val1
val1 = min(self.length-1, max(0, self.length-val1_-val2 ))
val2 = max(1, (self.length-1-val1_)-(val1-1) )
}
return self.bridgeToObjectiveC().substringWithRange(NSMakeRange(val1, val2))
//-- Alternative code without bridge --
//var range: Range<Int> = pos...(pos+len-1)
//var start = advance(startIndex, range.startIndex)
//var end = advance(startIndex, range.endIndex)
//return self.substringWithRange(Range(start: start, end: end))
}
}
println("0123456789"[3]) // return "3"
println("0123456789"[3,2]) // return "34"
println("0123456789"[3,2,.right2left]) // return "56"
println("0123456789"[5,10,.pos_len,.left2right]) // return "56789"
println("0123456789"[8,120,.pos_len,.right2left]) // return "01"
println("0123456789"[120,120,.pos_len,.left2right]) // return "9"
println("0123456789"[0...4]) // return "01234"
println("0123456789"[0..4]) // return "0123"
println("0123456789"[0...4,.right2left]) // return "56789"
println("0123456789"[4...0,.right2left]) // return "678" << because ??? range can wear endIndex at 0 ???
Swift 4.2或更高版本
使用String的indexes属性进行范围和部分范围下标
作为@LeoDabus nice answer的变体,我们可以为DefaultIndices添加一个额外的扩展,目的是允许我们在为后者实现自定义下标(通过Int专用范围和部分范围)时使用String的indexes属性。
extension DefaultIndices {
subscript(at: Int) -> Elements.Index { index(startIndex, offsetBy: at) }
}
// Moving the index(_:offsetBy:) to an extension yields slightly
// briefer implementations for these String extensions.
extension String {
subscript(range: Range<Int>) -> SubSequence {
let start = indices[range.lowerBound]
return self[start..<indices[start...][range.count]]
}
subscript(range: ClosedRange<Int>) -> SubSequence {
let start = indices[range.lowerBound]
return self[start...indices[start...][range.count]]
}
subscript(range: PartialRangeFrom<Int>) -> SubSequence {
self[indices[range.lowerBound]...]
}
subscript(range: PartialRangeThrough<Int>) -> SubSequence {
self[...indices[range.upperBound]]
}
subscript(range: PartialRangeUpTo<Int>) -> SubSequence {
self[..<indices[range.upperBound]]
}
}
let str = "foo bar baz bax"
print(str[4..<6]) // "ba"
print(str[4...6]) // "bar"
print(str[4...]) // "bar baz bax"
print(str[...6]) // "foo bar"
print(str[..<6]) // "foo ba"
感谢@LeoDabus指出我在使用索引属性作为字符串下标的(其他)替代方案的方向!
看第一个字母:
first(str) // retrieve first letter
更多: http://sketchytech.blogspot.com/2014/08/swift-pure-swift-method-for-returning.html
Swift3
可以使用下标语法访问特定String索引处的Character。
let greeting = "Guten Tag!"
let index = greeting.index(greeting.startIndex, offsetBy: 7)
greeting[index] // a
访问https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html
或者我们可以在Swift 4中做一个字符串扩展
extension String {
func getCharAtIndex(_ index: Int) -> Character {
return self[self.index(self.startIndex, offsetBy: index)]
}
}
用法:
let foo = "ABC123"
foo.getCharAtIndex(2) //C
斯威夫特4
let str = "My String"
索引处的字符串
let index = str.index(str.startIndex, offsetBy: 3)
String(str[index]) // "S"
子字符串
let startIndex = str.index(str.startIndex, offsetBy: 3)
let endIndex = str.index(str.startIndex, offsetBy: 7)
String(str[startIndex...endIndex]) // "Strin"
前n个字符
let startIndex = str.index(str.startIndex, offsetBy: 3)
String(str[..<startIndex]) // "My "
最后n个字符
let startIndex = str.index(str.startIndex, offsetBy: 3)
String(str[startIndex...]) // "String"
Swift 2和3
str = "My String"
**字符串索引**
斯威夫特2
let charAtIndex = String(str[str.startIndex.advancedBy(3)]) // charAtIndex = "S"
斯威夫特3
str[str.index(str.startIndex, offsetBy: 3)]
子字符串fromIndex toIndex
斯威夫特2
let subStr = str[str.startIndex.advancedBy(3)...str.startIndex.advancedBy(7)] // subStr = "Strin"
斯威夫特3
str[str.index(str.startIndex, offsetBy: 3)...str.index(str.startIndex, offsetBy: 7)]
前n个字符
let first2Chars = String(str.characters.prefix(2)) // first2Chars = "My"
最后n个字符
let last3Chars = String(str.characters.suffix(3)) // last3Chars = "ing"