Update for Swift 4
In Swift 4, String
conforms to Collection
again, so it is possible to use dropFirst
and dropLast
to trim the beginnings and ends of strings. The result is of type Substring
, so you need to pass that to the String
constructor to get back a String
:
let str = "hello"
let result1 = String(str.dropFirst()) // "ello"
let result2 = String(str.dropLast()) // "hell"
dropFirst()
and dropLast()
also take an Int
to specify the number of characters to drop:
let result3 = String(str.dropLast(3)) // "he"
let result4 = String(str.dropFirst(4)) // "o"
If you specify more characters to drop than are in the string, the result will be the empty string (""
).
let result5 = String(str.dropFirst(10)) // ""
Update for Swift 3
If you just want to remove the first character and want to change the original string in place, then see @MickMacCallum's answer. If you want to create a new string in the process, use substring(from:)
. With an extension to String
, you can hide the ugliness of substring(from:)
and substring(to:)
to create useful additions to trim the start and ends of a String
:
extension String {
func chopPrefix(_ count: Int = 1) -> String {
return substring(from: index(startIndex, offsetBy: count))
}
func chopSuffix(_ count: Int = 1) -> String {
return substring(to: index(endIndex, offsetBy: -count))
}
}
"hello".chopPrefix() // "ello"
"hello".chopPrefix(3) // "lo"
"hello".chopSuffix() // "hell"
"hello".chopSuffix(3) // "he"
Like dropFirst
and dropLast
before them, these functions will crash if there aren't enough letters available in the String. The onus is on the caller to use them properly. This is a valid design decision. One could write them to return an optional which then would have to be unwrapped by the caller.
Swift 2.x
Alas in Swift 2, dropFirst
and dropLast
(the previous best solution) aren't as convenient as they were before. With an extension to String
, you can hide the ugliness of substringFromIndex
and substringToIndex
:
extension String {
func chopPrefix(count: Int = 1) -> String {
return self.substringFromIndex(advance(self.startIndex, count))
}
func chopSuffix(count: Int = 1) -> String {
return self.substringToIndex(advance(self.endIndex, -count))
}
}
"hello".chopPrefix() // "ello"
"hello".chopPrefix(3) // "lo"
"hello".chopSuffix() // "hell"
"hello".chopSuffix(3) // "he"
Like dropFirst
and dropLast
before them, these functions will crash if there aren't enough letters available in the String. The onus is on the caller to use them properly. This is a valid design decision. One could write them to return an optional which then would have to be unwrapped by the caller.
In Swift 1.2, you'll need to call chopPrefix
like this:
"hello".chopPrefix(count: 3) // "lo"
or you can add an underscore _
to the function definitions to suppress the parameter name:
extension String {
func chopPrefix(_ count: Int = 1) -> String {
return self.substringFromIndex(advance(self.startIndex, count))
}
func chopSuffix(_ count: Int = 1) -> String {
return self.substringToIndex(advance(self.endIndex, -count))
}
}
advance
thing by castingdisplay.text!
to NSString. I'm not saying that's a good solution - just correcting a possible misconception. With NSString, you can index into it with Int. - And the reason you can't index with Int is not because of Unicode; it's because a Character can consist of multiple compound codepoints.String
then Swift 3 has introduced thecapitalized
function toString
.