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

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -