Tuesday, 14 June 2016

Map weather a string contains all same character or not and can also replace with other character

In This example we are replacing "0" with A, You can edit as per your requirement


let numberStr = "0000" // Here you set your string
let arrayfirst = numberStr.characters.filter{$0 == "0"}
// This is to check weather coming string is only having "0" or any other character only or not, 
            if numberStr.characters.count ==  arrayfirst.count { // If yes then this condition will satisfy
                let array = numberStr.characters.map { (value) -> String in
                    if value == "0" {
                        return "A" // Symbol for arabic zero
                    }
                    return ""
                }
                return array.joinWithSeparator("")
            }
Out Put: "AAAA"