Disemvowel in swift -
we can remove of vowels string in javascript this:
function disemvowel(str) { str = str.replace(/([aeiouaeiou])/g, '') return str; } i implement same function in swift, curious how can write shorter javascript?
func disemvowelthestring(string: string) -> string { var replacedstring = string let vowels = ["a", "e", "i", "o", "u", "a", "e", "i", "o", "u"] vowel in vowels { if string.containsstring(vowel) { replacedstring = replacedstring.stringbyreplacingoccurrencesofstring(vowel, withstring: "") } } return replacedstring }
one option filter vowels input string's characters:
func removevowels(input: string) -> string { let vowels: [character] = ["a", "e", "i", "o", "u", "a", "e", "i", "o", "u"] let result = string(input.characters.filter { !vowels.contains($0) }) return result }
Comments
Post a Comment