How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator
You should leave one side empty, hence the name "partial range".
let newStr = str[..<index]
The same stands for partial range from operators, just leave the other side empty:
let newStr = str[index...]
Keep in mind that these range operators return a Substring
. If you want to convert it to a string, use String
's initialization function:
let newStr = String(str[..<index])
You can read more about the new substrings here.
'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator
I would say localNumber.prefix(3)
in this situation. Short and sweet.
substring(from:)' is deprecated. Use string slicing subscript with a 'partial range from' operator
swift 4 has prefix and suffix just for this:
let contact_phone = "0123456789"
let t_prefix_phone = String(contact_phone.prefix(3))
let t_phone = String(contact_phone.suffix(7))
'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator. Swift 4 Error
'characters' is deprecated: Please use String or Substring directly
For this you can directly loop through string in swift 4 so there is no need to characters array.
And for
'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator.
Refer How can I use String slicing subscripts in Swift 4?
Swift 4 : substring(with:)' is deprecated: Please use String slicing subscript
The someString.substring(with: someRange)
just needs to be someString[someRange]
.
So change:
entity.substring(with: entity.index(entity.startIndex, offsetBy: 3) ..< entity.index(entity.endIndex, offsetBy: -1))
with
entity[entity.index(entity.startIndex, offsetBy: 3) ..< entity.index(entity.endIndex, offsetBy: -1)]
In other words, change the .substring(with:
to [
and change the closing )
to ]
.
The result is a Substring
, not String
. So you may need to wrap the result in String( )
to get a String
from the substring result.
Swift 4 - 'substring(to:)' is deprecated - Alternative To My Code
Simply call prefix(_:)
on theReview
with the required length
.
func prefix(_ maxLength: Int) -> Substring
Returns a subsequence, up to the specified maximum length, containing
the initial elements of the collection.If the maximum length exceeds the number of elements in the
collection, the result contains all the elements in the collection
var theReview = addReview.text
theReview = String(theReview.prefix(3000))
Note: There is no need to check if the theReview's length
exceeds 3000
. It will be handled by prefix(_:)
itself.
How to use String subscript in Swift 4
You should have:
extension String {
func substring(_ from: Int) -> String {
let start = index(startIndex, offsetBy: from)
return String(self[start ..< endIndex])
}
}
In swift you can get a substring using slicing - a construction like this: string[startIndex ..< endIndex]
. String index is a special type in swift so you can't use simple ints - you have to get proper indexes for instance calling index(_,offsetBy:)
or using predefined startIndex
and endIndex
.
Related Topics
Overriding Methods in Swift Extensions
How to Add Type Constraints to a Swift Protocol Conformance Extension
What Is the 'Open' Keyword in Swift
Getting the Decimal Part of a Double in Swift
Transparent Background For Modally Presented Viewcontroller
How to Append Elements into a Dictionary in Swift
How to Get a Real Ip Address from Dns Query in Swift
Index of a Substring in a String With Swift
Uitableview With More Than One Custom Cells With Swift
Share Data Between Main App and Widget in Swiftui For iOS 14
How to Declare an Array of Weak References in Swift
Programmatically Detect Tab Bar or Tabview Height in Swiftui