Get nth character of a string in Swift programming language

后端 未结 30 2012
一整个雨季
一整个雨季 2020-11-22 01:26

How can I get the nth character of a string? I tried bracket([]) accessor with no luck.

var string = \"Hello, world!\"

var firstChar = string[         


        
30条回答
  •  悲哀的现实
    2020-11-22 01:54

    Get & Set Subscript (String & Substring) - Swift 4.2

    Swift 4.2, Xcode 10

    I based my answer off of @alecarlson's answer. The only big difference is you can get a Substring or a String returned (and in some cases, a single Character). You can also get and set the subscript. Lastly, mine is a bit more cumbersome and longer than @alecarlson's answer and as such, I suggest you put it in a source file.


    Extension:

    public extension String {
        public subscript (i: Int) -> Character {
            get {
                return self[index(startIndex, offsetBy: i)]
            }
            set (c) {
                let n = index(startIndex, offsetBy: i)
                replaceSubrange(n...n, with: "\(c)")
            }
        }
        public subscript (bounds: CountableRange) -> Substring {
            get {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return self[start ..< end]
            }
            set (s) {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(start ..< end, with: s)
            }
        }
        public subscript (bounds: CountableClosedRange) -> Substring {
            get {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return self[start ... end]
            }
            set (s) {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(start ... end, with: s)
            }
            
        }
        public subscript (bounds: CountablePartialRangeFrom) -> Substring {
            get {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(endIndex, offsetBy: -1)
                return self[start ... end]
            }
            set (s) {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(endIndex, offsetBy: -1)
                replaceSubrange(start ... end, with: s)
            }
        }
        public subscript (bounds: PartialRangeThrough) -> Substring {
            get {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return self[startIndex ... end]
            }
            set (s) {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(startIndex ... end, with: s)
            }
        }
        public subscript (bounds: PartialRangeUpTo) -> Substring {
            get {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return self[startIndex ..< end]
            }
            set (s) {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(startIndex ..< end, with: s)
            }
        }
        
        public subscript (i: Int) -> String {
            get {
                return "\(self[index(startIndex, offsetBy: i)])"
            }
            set (c) {
                let n = index(startIndex, offsetBy: i)
                self.replaceSubrange(n...n, with: "\(c)")
            }
        }
        public subscript (bounds: CountableRange) -> String {
            get {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return "\(self[start ..< end])"
            }
            set (s) {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(start ..< end, with: s)
            }
        }
        public subscript (bounds: CountableClosedRange) -> String {
            get {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return "\(self[start ... end])"
            }
            set (s) {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(start ... end, with: s)
            }
            
        }
        public subscript (bounds: CountablePartialRangeFrom) -> String {
            get {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(endIndex, offsetBy: -1)
                return "\(self[start ... end])"
            }
            set (s) {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(endIndex, offsetBy: -1)
                replaceSubrange(start ... end, with: s)
            }
        }
        public subscript (bounds: PartialRangeThrough) -> String {
            get {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return "\(self[startIndex ... end])"
            }
            set (s) {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(startIndex ... end, with: s)
            }
        }
        public subscript (bounds: PartialRangeUpTo) -> String {
            get {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return "\(self[startIndex ..< end])"
            }
            set (s) {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(startIndex ..< end, with: s)
            }
        }
        
        public subscript (i: Int) -> Substring {
            get {
                return Substring("\(self[index(startIndex, offsetBy: i)])")
            }
            set (c) {
                let n = index(startIndex, offsetBy: i)
                replaceSubrange(n...n, with: "\(c)")
            }
        }
    }
    public extension Substring {
        public subscript (i: Int) -> Character {
            get {
                return self[index(startIndex, offsetBy: i)]
            }
            set (c) {
                let n = index(startIndex, offsetBy: i)
                replaceSubrange(n...n, with: "\(c)")
            }
            
        }
        public subscript (bounds: CountableRange) -> Substring {
            get {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return self[start ..< end]
            }
            set (s) {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(start ..< end, with: s)
            }
        }
        public subscript (bounds: CountableClosedRange) -> Substring {
            get {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return self[start ... end]
            }
            set (s) {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(start ... end, with: s)
            }
        }
        public subscript (bounds: CountablePartialRangeFrom) -> Substring {
            get {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(endIndex, offsetBy: -1)
                return self[start ... end]
            }
            set (s) {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(endIndex, offsetBy: -1)
                replaceSubrange(start ... end, with: s)
            }
            
        }
        public subscript (bounds: PartialRangeThrough) -> Substring {
            get {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return self[startIndex ... end]
            }
            set (s) {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(startIndex ..< end, with: s)
            }
        }
        public subscript (bounds: PartialRangeUpTo) -> Substring {
            get {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return self[startIndex ..< end]
            }
            set (s) {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(startIndex ..< end, with: s)
            }
        }
        public subscript (i: Int) -> String {
            get {
                return "\(self[index(startIndex, offsetBy: i)])"
            }
            set (c) {
                let n = index(startIndex, offsetBy: i)
                replaceSubrange(n...n, with: "\(c)")
            }
        }
        public subscript (bounds: CountableRange) -> String {
            get {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return "\(self[start ..< end])"
            }
            set (s) {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(start ..< end, with: s)
            }
        }
        public subscript (bounds: CountableClosedRange) -> String {
            get {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return "\(self[start ... end])"
            }
            set (s) {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(start ... end, with: s)
            }
            
        }
        public subscript (bounds: CountablePartialRangeFrom) -> String {
            get {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(endIndex, offsetBy: -1)
                return "\(self[start ... end])"
            }
            set (s) {
                let start = index(startIndex, offsetBy: bounds.lowerBound)
                let end = index(endIndex, offsetBy: -1)
                replaceSubrange(start ... end, with: s)
            }
        }
        public subscript (bounds: PartialRangeThrough) -> String {
            get {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return "\(self[startIndex ... end])"
            }
            set (s) {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(startIndex ... end, with: s)
            }
        }
        public subscript (bounds: PartialRangeUpTo) -> String {
            get {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                return "\(self[startIndex ..< end])"
            }
            set (s) {
                let end = index(startIndex, offsetBy: bounds.upperBound)
                replaceSubrange(startIndex ..< end, with: s)
            }
        }
        
        public subscript (i: Int) -> Substring {
            get {
                return Substring("\(self[index(startIndex, offsetBy: i)])")
            }
            set (c) {
                let n = index(startIndex, offsetBy: i)
                replaceSubrange(n...n, with: "\(c)")
            }
        }
    }
    

提交回复
热议问题