Reputation: 3636
I tried with this code:
let vowels: [Character] = ["a","e","i","o","u", "y"]
let replaced = String(myString.map {
$0 == vowels.contains($0) ? "1" : "0"
})
But I have the error:
Binary operator '==' cannot be applied to operands of type 'Character' and 'Bool'
What is wrong?
Upvotes: 2
Views: 1420
Reputation: 568
different way but it is still as you want
let strs = "hello world"
var str = String()
let vowles = ["e","o"]
//if you want the index also use this (index,char) in strs.enumerated()
//if the index is not important use this char in strs
for (index,char) in strs.enumerated()
{
var who = String(char)
if vowles.contains(who){
print(who)
//replace or deleted by changing the value of who
who = ""
str = str + who
print(str)
}else{
str = str + who
}
}
Upvotes: -1
Reputation: 4827
replace all vowels in a String with asterisk symbol
let vowels: [Character] = ["a","e","i","o","u", "y"]
var myString = "mahipal singh"
let replaced = String(myString.map {
vowels.contains($0) ? Character("*") : $0 // Replace * with your character you wanna to replace
})
print(replaced)
Upvotes: 1
Reputation: 811
let vowels: [Character] = ["a","e","i","o","u","y","A","E","I","O","U", "Y"]
var replaced = String()
for char in myString {
if !vowels.contains(char){
replaced = "\(replaced)\(char)"
}
}
Upvotes: 0
Reputation: 5331
You can remove vowel from string like so:
string.remove(at: string.index(where: {vowels.contains($0)})!)
Upvotes: 0
Reputation: 9923
Just need to replace $0 ==
with return
, you were comparing Character with Bool it makes no sense
let vowels: [Character] = ["a","e","i","o","u", "y"]
let replaced = String(myString.map {
return vowels.contains($0) ? "1" : "0"
})
Upvotes: 5