How to replace alphanumeric values in string in iOS? -


i want replace alphanumeric characters except white spaces in string in ios. compact solution?

this recent approach:

var strin = "apple banana mushroom" let = (strin).componentsseparatedbystring(" ")  var strreturn: nsstring str in {      let str3 = "".stringbypaddingtolength((str nsstring).length, withstring: "*", startingatindex: 0)     strreturn = strreturn.stringbyappendingstring(str3) } 

expected result is: "****** ****** ********"

a simple version that:

let strin = "apple banana mushroom" let words = strin.componentsseparatedbystring(" ")  var starred: [string] = []  word in words {     let stars = array(count: word.characters.count, repeatedvalue: "*").joinwithseparator("")     starred.append(stars) }  let result = starred.joinwithseparator(" ") 

result:

"***** ****** ********"

you can make extension easier usage:

extension string {     var stars:string {         let words = self.componentsseparatedbystring(" ")         var starred: [string] = []         word in words {             let stars = array(count: word.characters.count, repeatedvalue: "*").joinwithseparator("")             starred.append(stars)         }         return starred.joinwithseparator(" ")     } }  let starred = "apple banana mushroom".stars 

you can replace loop map , produce kind of monster one-liner. it's efficient not readable, use @ own discretion:

extension string {     var stars:string {         return self.componentsseparatedbystring(" ").map { array(count: $0.characters.count, repeatedvalue: "*").joinwithseparator("") }.joinwithseparator(" ")     } }  let starred = "apple banana mushroom".stars 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -