Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use substringToIndex in Swift? [duplicate]

I get compiler error at this line:

UIDevice.currentDevice().identifierForVendor.UUIDString.substringToIndex(8) 

Type 'String.Index' does not conform to protocol 'IntegerLiteralConvertible'

My intention is to get the substring, but how?

like image 954
János Avatar asked Jul 23 '14 07:07

János


People also ask

How to get a substring from string in Swift?

You can get a substring from a string by using subscripts or a number of other methods (for example, prefix , suffix , split ). You still need to use String. Index and not an Int index for the range, though.

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 index of a character in a string in Swift?

In swift, we can use the firstIndex(of:) method to get the index position of a character in a given string.

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

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


1 Answers

In Swift, String indexing respects grapheme clusters, and an IndexType is not an Int. You have two choices - cast the string (your UUID) to an NSString, and use it as "before", or create an index to the nth character.

Both are illustrated below :

However, the method has changed radically between versions of Swift. Read down for later versions...

Swift 1

let s: String = "Stack Overflow" let ss1: String = (s as NSString).substringToIndex(5) // "Stack" //let ss2: String = s.substringToIndex(5)// 5 is not a String.Index let index: String.Index = advance(s.startIndex, 5) let ss2:String = s.substringToIndex(index) // "Stack" 

CMD-Click on substringToIndex confusingly takes you to the NSString definition, however CMD-Click on String and you will find the following:

extension String : Collection {     struct Index : BidirectionalIndex, Reflectable {         func successor() -> String.Index         func predecessor() -> String.Index         func getMirror() -> Mirror     }     var startIndex: String.Index { get }     var endIndex: String.Index { get }     subscript (i: String.Index) -> Character { get }     func generate() -> IndexingGenerator<String> } 

Swift 2
As commentator @DanielGalasko points out advance has now changed...

let s: String = "Stack Overflow" let ss1: String = (s as NSString).substringToIndex(5) // "Stack" //let ss2: String = s.substringToIndex(5)// 5 is not a String.Index let index: String.Index = s.startIndex.advancedBy(5) // Swift 2 let ss2:String = s.substringToIndex(index) // "Stack" 

Swift 3
In Swift 3, it's changed again:

let s: String = "Stack Overflow" let ss1: String = (s as NSString).substring(to: 5) // "Stack" let index: String.Index = s.index(s.startIndex, offsetBy: 5) var ss2: String = s.substring(to: index) // "Stack" 

Swift 4
In Swift 4, yet another change:

let s: String = "Stack Overflow" let ss1: String = (s as NSString).substring(to: 5) // "Stack" let index: String.Index = s.index(s.startIndex, offsetBy: 5) var ss3: Substring = s[..<index] // "Stack" var ss4: String = String(s[..<index]) // "Stack" 
like image 196
Grimxn Avatar answered Sep 19 '22 00:09

Grimxn