Length of String as extension of String

后端 未结 2 445
独厮守ぢ
独厮守ぢ 2021-01-22 23:18

I know that there will be lots of pointers to duplicates but this worked before I updated to xcode 6.3 and now it has a problem with it.

The script:

exte         


        
2条回答
  •  故里飘歌
    2021-01-22 23:32

    var str = "Hello, playground"
    
    extension String {
        func removeCharsFromEnd(n:Int) -> String {
            return substringWithRange(Range(start: startIndex, end: advance(startIndex, count(self) < n ? 0 : count(self)-n )))
        }
    }
    
    "Hello, playground".removeCharsFromEnd(3)  // "Hello, playgro"
    "

提交回复
热议问题