What is the most succinct way to remove the first character from a string in Swift?
If you're using Swift 3, you can ignore the second section of this answer. Good news is, this is now actually succinct again! Just using String's new remove(at:) method.
var myString = "Hello, World"
myString.remove(at: myString.startIndex)
myString // "ello, World"
I like the global dropFirst()
function for this.
let original = "Hello" // Hello
let sliced = dropFirst(original) // ello
It's short, clear, and works for anything that conforms to the Sliceable protocol.
If you're using Swift 2, this answer has changed. You can still use dropFirst, but not without dropping the first character from your strings characters
property and then converting the result back to a String. dropFirst has also become a method, not a function.
let original = "Hello" // Hello
let sliced = String(original.characters.dropFirst()) // ello
Another alternative is to use the suffix function to splice the string's UTF16View
. Of course, this has to be converted back to a String afterwards as well.
let original = "Hello" // Hello
let sliced = String(suffix(original.utf16, original.utf16.count - 1)) // ello
All this is to say that the solution I originally provided has turned out not to be the most succinct way of doing this in newer versions of Swift. I recommend falling back on @chris' solution using removeAtIndex()
if you're looking for a short and intuitive solution.
var original = "Hello" // Hello
let removedChar = original.removeAtIndex(original.startIndex)
original // ello
And as pointed out by @vacawama in the comments below, another option that doesn't modify the original String is to use substringFromIndex.
let original = "Hello" // Hello
let substring = original.substringFromIndex(advance(original.startIndex, 1)) // ello
Or if you happen to be looking to drop a character off the beginning and end of the String, you can use substringWithRange. Just be sure to guard against the condition when startIndex + n > endIndex - m
.
let original = "Hello" // Hello
let newStartIndex = advance(original.startIndex, 1)
let newEndIndex = advance(original.endIndex, -1)
let substring = original.substringWithRange(newStartIndex..<newEndIndex) // ell
The last line can also be written using subscript notation.
let substring = original[newStartIndex..<newEndIndex]
Remove the first six characters from a String (Swift)
length
is the number of characters you want to remove (6 in your case)
extension String {
func toLengthOf(length:Int) -> String {
if length <= 0 {
return self
} else if let to = self.index(self.startIndex, offsetBy: length, limitedBy: self.endIndex) {
return self.substring(from: to)
} else {
return ""
}
}
}
How to get the first character of each word in a string?
You can try this code:
let stringInput = "First Last"
let stringInputArr = stringInput.components(separatedBy:" ")
var stringNeed = ""
for string in stringInputArr {
stringNeed += String(string.first!)
}
print(stringNeed)
If have problem with componentsSeparatedByString
you can try seperate by character space and continue in array you remove all string empty.
Hope this help!
How do I take a substring to the first index of a character?
Something like this should work:
myMessage.substringToIndex(myMessage.characters.indexOf(" ")!)
Note that in this code I force unwrapped the optional. If you're not guaranteed to have that space in the string, it might make more sense to have the index in a optional binding.
With optional binding, it would look something like this:
if let index = myMessage.characters.indexOf(" ") {
let result = myMessage.substringToIndex(index)
}
Add a space in a String after the first letter - Swift 5
var greeting = "Hello, playground"
greeting.insert(" ", at: greeting.index(after: greeting.startIndex))
print(greeting) // H ello, playground
greeting.insert(" ", at: greeting.index(greeting.startIndex, offsetBy: 1))
print(greeting) // H ello, playground
Better way to find occurrence amount of a certain character in a string
One possible solution:
let string = "xhjklghxhjkjjklxjhjkjxx"
print(string.filter({ $0 == "x" }).count)
// prints: 5
Related Topics
Swift - 'Bool' Is Not a Subtype of 'Void'
How to Move a Rotated Scnnode in Scenekit
Swift 4 Timer Crashes with Nsexception
Use 'Self' as a Default Parameter
What Are the Precedence Levels of the Swift Operators
Why Is Forwarding Variadic Parameters Invalid
Swift 3:Issue with Avvideocompositioncoreanimationtool to Add Watermark on Video
How to Work with Bindings When Using a View Model VS Using @Binding in the View Itself
How to Block Users on Firebase in a Social Media App? for iOS
Early Return from a Void-Func in Swift
Swift: Does Closure Have References to Constants or Variables
How to Determine If a Variable Passed in Is Reference Type or Value Type
How to Check If 'Any(Not Any)' Is Nil or Not in Swift
Why 'There Cannot Be More Than One Conformance, Even with Different Conditional Bounds'