Is there a quick way to remove the last two characters in a String in Swift 3.0? I see there is a simple way to remove the last character as clearly noted here. Do you know how to remove the last two characters? Thanks!
相关问题
- how to split a list into a given number of sub-lis
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- JSP String formatting Truncate
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Selecting only the first few characters in a strin
- Enum with associated value conforming to CaseItera
update: Xcode 9 • Swift 4 or later
Now String conforms to RangeReplaceableCollection so now you can use Array's method dropLast straight in the String and therefore an extension it is not necessary anymore. The only difference is that it returns a Substring. If you need a String you need to initialize a new one from it:
Swift 3.x
You can use the method dropLast(n:) on the characters to remove any number of characters:
As an extension:
Better to use removeLast()
Use
removeSubrange(Range<String.Index>)
just like:This will crash if the string is less than 2 characters long. Is that a requirement for you?
swift 4: