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(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.
Swift 5: 'substring(to:)' is deprecated
Use String Slicing
instead of Substring
. like below
let yourString = "dummy string"
yourString[5..<yourString.count] // string
auto line numbering UITextView in swift 5
Please try below line in your code.
let str = textView.text[..<textView.text.index(textView.text.endIndex, offsetBy: -4)]
Related Topics
Swiftier Swift for 'Add to Array, or Create If Not There...'
Get Current Time as String Swift 3.0
Accessing Mkmapview Elements as Uiviewrepresentable in the Main (Contentview) Swiftui View
Swiftui Exporting or Sharing Files
Swift 2.0 - 'Nil' or '0' Enum Arguments
Implementing a Function with a Default Parameter Defined in a Protocol
How to Test If Objects Conforming to the Same Protocol Are Identical in Swift Without Casting
Protocol Extending Encodable (Or Codable) Does Not Conform to It
Sprite Kit Physicsbody.Resting Behavior
What Do "_" and "In" Mean in Swift Programming Language
Unsaferawpointer Assumingmemorybound VS. Bindmemory
How to Get the Height of a Uilabel in Swift
Anonymous Closure Argument Not Contained in a Closure
How to Filter Characters from a String in Swift 4
Swift Http Request Use Urlsession
Autolayout Contraints for a View from Xib
Uicollectionviewlayout Not Working with Uiimage in Swift 5 and Xcode 11