Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator

How can I update the following code to the new version of swift:

self.areaCodeLael.text! = localNumber.substring(to: localNumber.index(localNumber.startIndex, offsetBy: 3))

I have tried following this post but I can't get it right How can I use String slicing subscripts in Swift 4?

I adjusted my original code to localNumber[..<3] but I get:

Cannot subscript a value of type 'String' with an index of type 'PartialRangeUpTo'

like image 800
user1079052 Avatar asked Aug 28 '17 15:08

user1079052


People also ask

How do you slice a string in Swift?

In Swift 4 you slice a string into a substring using subscripting. The use of substring(from:) , substring(to:) and substring(with:) are all deprecated.

How do you get the first character of a string in Swift 5?

In Swift, the first property is used to return the first character of a string.

How do I remove the first character from a string in Swift?

Swift String dropFirst() The dropFirst() method removes the first character of the string.

What is substring Swift?

Substrings. When you get a substring from a string—for example, using a subscript or a method like prefix(_:) —the result is an instance of Substring , not another string. Substrings in Swift have most of the same methods as strings, which means you can work with substrings the same way you work with strings.


1 Answers

I would say localNumber.prefix(3) in this situation. Short and sweet.

like image 128
matt Avatar answered Oct 20 '22 23:10

matt