Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator

Tags:

swift

swift4

I have the following simple code written in Swift 3:

let str = "Hello, playground" let index = str.index(of: ",")! let newStr = str.substring(to: index) 

From Xcode 9 beta 5, I get the following warning:

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

How can this slicing subscript with partial range from be used in Swift 4?

like image 759
Adrian Avatar asked Aug 08 '17 08:08

Adrian


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 get the length of a string in Swift?

Swift – String Length/Count To get the length of a String in Swift, use count property of the string. count property is an integer value representing the number of characters in this 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.

How to find substring in Swift?

How to Find SubString in Swift? To find substring of a String in Swift, prepare Range object using start and end indices, then give this Range object to the string in square brackets as an index. The syntax to concatenate two strings is:

How do you concatenate two strings in Swift?

Swift Substring. To find substring of a String in Swift, prepare range using start and end indexes, then use the range on the string. The syntax to concatenate two strings is: where startPosition and endPosition are integers that define the bounds of the substring in the main string str.

What is string slicing in Swift?

String slicing is somewhat verbose in Swift. If you’re coming from another language, it’s not immediately obvious how to do it at all. Many of us end up cursing the screen after trying to get substrings using patterns from other languages.

Why do Swift strings use non-integer index types?

Swift Strings use a non-integer index type for reasons of speed and safety: Speed: because an index access will always be a constant-time operation. Safety: because you’re guaranteed to get a whole character and not, say, half of a 16-bit character. I know what you’re thinking: that’s swell and all, but I just want to grab a range of characters.


2 Answers

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.

like image 51
Tamás Sengel Avatar answered Oct 16 '22 16:10

Tamás Sengel


Convert Substring (Swift 3) to String Slicing (Swift 4)

Examples In Swift 3, 4:

let newStr = str.substring(to: index) // Swift 3 let newStr = String(str[..<index]) // Swift 4 

let newStr = str.substring(from: index) // Swift 3 let newStr = String(str[index...]) // Swift 4  

let range = firstIndex..<secondIndex // If you have a range let newStr = = str.substring(with: range) // Swift 3 let newStr = String(str[range])  // Swift 4 
like image 24
Mohammad Sadegh Panadgoo Avatar answered Oct 16 '22 17:10

Mohammad Sadegh Panadgoo