DrwMay DrwMay - 3 months ago 119
Swift Question

Swift 3.0 How to get a substring from a string

Evening, I know this is a so basic question, how can I find a substring?

Should I use

CharactersView
?

Let's assume we have this String:
let date: String = "08/23/2016"


And I want to catch month, day and year in separated string, how can I do?

enter image description here

ddb ddb
Answer

this way

let array = date.characters.split(separator: "/").map(String.init)

let month = array[0];
let day = array[1];
let year = array[2];

EDIT

or this way (working on Swift 3)

let array = date.components(separatedBy: "/")

let month = array[0];
let day = array[1];
let year = array[2];