Skip to content

Instantly share code, notes, and snippets.

@Pasanpr
Created February 16, 2017 17:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Pasanpr/26e6e6227e62b7330a09a825c02a612f to your computer and use it in GitHub Desktop.
Save Pasanpr/26e6e6227e62b7330a09a825c02a612f to your computer and use it in GitHub Desktop.
Determine if a character is a vowel or not
extension Character {
var isVowel: Bool {
let vowels: [Character] = ["a", "e", "i", "o", "u"]
return vowels.contains(self)
}
}
@syntakks
Copy link

syntakks commented Jun 12, 2018

I added the uppercased versions to the Character Extension, as well as an isConsonant that just piggy backs off of isVowel so you don't have to loop through all the other characters.

extension Character {
    var isVowel: Bool {
        let vowels: [Character] = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
        return vowels.contains(self)
    }
    var isConsonant: Bool {
        return !self.isVowel
    }
}

func removeVowels(from string: String) -> String {
    var result: String = ""
    for character: Character in string.characters {
        if character.isConsonant {
            result.append(character)
        }
    }
    return result
}

@byaruhaf
Copy link

byaruhaf commented Mar 8, 2019

'characters' is deprecated in the newer versions of swift thus removeVowels function should:

func removeVowels(from value: String) -> String {
  return String(value.filter { !["a", "e", "i", "o", "u"].contains($0) })
}

@tjhv
Copy link

tjhv commented Jul 4, 2019

extension Character
{
    var isVowel: Bool {
        return "aeiou".contains(self)
    }
}

func removeVowels(from: String) -> String
{
    return from.filter { !$0.isVowel }
}

@marsdev26
Copy link

func removeVowels(from mystring: String) -> String {
    let vowels:[Character] = ["a","e","i","o","u","y"]
    var output = ""
    for a in mystring {
        if vowels.contains(a) != true {
            output += String(a)
        }
    }
    return output
 }

@IsaacRichardson
Copy link

func removeVowels(from value: String) -> String {
    var vowels: [Character] = ["a", "e", "i", "o", "u"]
    
    var newValue = value.lowercased()
    
    newValue.removeAll(where: { vowels.contains($0) })

    return newValue
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment