xcode - Swift adding multiple stringByReplacingOccurrencesOfString on one String? -


hello create app changes characters binary code , wondering if there way add multiple stringbyreplacingoccurrencesofstring on 1 string or if should take approach "problem". here have far

func texttobinary(thestring: string) -> string {     return thestring.stringbyreplacingoccurrencesofstring("a",         withstring: "01100001") }  textarea.text = texttobinary(letterscombined)  // letterscombined string want turn binarycode. 

try this:

func texttobinary(thestring : string, radix : int = 2) -> string {     var result = ""     c in thestring.unicodescalars {         result += string(c.value, radix: radix) + " "     }      return result }  println(texttobinary("a")) println(texttobinary("abc", radix: 10)) println(texttobinary("€20", radix: 16)) println(texttobinary("😄")) 

(the last 1 smiley face somehow browser can't display it).

edit: if want pad strings 8-character long, try this:

let str = "00000000" + string(c.value, radix: radix) result += str.substringfromindex(advance(str.startindex, str.characters.count - 8)) + " " 

the first line adds 8 0 left of string. second line takes last 8 characters padded string.


Comments