osx - Programmatically formatting string in swift -
osx - Programmatically formatting string in swift -
i have strings
var arraywithstrings = ["813 - crying flute.mp3", "ark patrol - never.mp3"]
i want get
var stringtitle1 = "813" var stringnameoftrack1 = "cryingflute" var stringformat1 = "mp3" var stringtitle2 = "ark patrol" var stringnameoftrack2 = "never" var stringformat2 = "mp3"
how programmatically?
you can utilize regular expressions:
let string = "813 - crying flute.mp3" allow regex = nsregularexpression(pattern: "^(.*) - (.*)\\.(.*)$", options: nil, error: nil) if allow matches = regex?.firstmatchinstring(string, options: nil, range: nsmakerange(0, count(string))) { allow title = (string nsstring).substringwithrange(matches.rangeatindex(1)) allow name = (string nsstring).substringwithrange(matches.rangeatindex(2)) allow format = (string nsstring).substringwithrange(matches.rangeatindex(3)) }
this uses "capturing parentheses" identify substrings, can identified rangeatindex
.
osx swift
Comments
Post a Comment