1

I'm trying to replace all vowels in a string with a "*"

This is what I have at the moment

string = "alphabet"

string.gsub! "a", "*"
string.gsub! "e", "*"
string.gsub! "i", "*"
string.gsub! "o", "*"
string.gsub! "u", "*"

I want string to equal "*lph*b*t". What's the easiest way to do this?

1

3 Answers 3

7

The easiest I can think of is:

string.tr!("aeiou", "*")
Sign up to request clarification or add additional context in comments.

Comments

5

The other standard way is:

string = "alphabet"
string.gsub!(/[aeiou]/,'*')
  #=> "*lph*b*t" 
string
  #=> "*lph*b*t" 

which you could also write

string.gsub!(/[aeiou]/) {'*'}

Comments

-1

In Javascript, Below is one of way to do this.

const vArr = 'aeiouy'.split('');

  const sArr = inputStr.split('');

  const binaryArray = sArr.map(i => {
    if(vArr.indexOf(i) > -1){
      return "0"; //I am trying to replace all vowels with zero
    } else {
      return i  //else replace with same char
    }
  })

  const resultStr = binaryArray.join('');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.