Difficulty trying to split a string in swift -
i have issue in swift when trying split bizzare string in swift
the string is:
qwerty.mapview
37.33233141 -122.0312186
tyrewq.mapview
37.33233141 -122.0312
how should if try make this
qwerty.mapview 37.31 -122.031
tyrewq.mapview 37.33 -122.032
i tried things hit issue because of starting string having \n after each word
i did tests in playground. following code should want. can write shorter, better explanation split every command 1 line..
var numberformatter = nsnumberformatter() numberformatter.maximumfractiondigits = 3 // on number formatter can define desired layout var teststring = "qwerty.mapview\n37.33233141 -122.0312186" var splitbynewline = teststring.componentsseparatedbystring("\n") var splitbyspace = splitbynewline[1].componentsseparatedbystring(" ") var nsstringlongitude = nsstring(string:splitbyspace[0]) var longitude = nsstringlongitude.floatvalue var nsstringlatitude = nsstring(string:splitbyspace[1]) var latitude = nsstringlatitude.floatvalue var formattedlongitude = numberformatter.stringfromnumber(longitude) var formattedlatitude = numberformatter.stringfromnumber(latitude) var finaloutput = "\(splitbynewline[0]) \(formattedlongitude) \(formattedlatitude)"
Comments
Post a Comment