Home > Software design >  Simple removeVowel function not changing input string
Simple removeVowel function not changing input string

Time:03-19

I'm trying to make a simple function in dart to test on which should remove all vowels from an input string but its seems my code never changes the from the original input. Could anyone help me with this? thanks

  String removeVowel( str) {
  var toReturn = "";
  for (var i = 0; i < str.length; i  ) {

    var temp = str.substring(i,i 1);

    if (temp != 'a' || temp != 'e' || temp != 'i' || temp != 'o' || temp!= 'u')
    {
      toReturn = toReturn   temp;
    }
  }
  return toReturn;
}

and what my tests shows:

00:02  0 -1: dog --> dg [E]
  Expected: 'dg'
    Actual: 'dog'
     Which: is different.
            Expected: dg
              Actual: dog
                       ^
             Differ at offset 1

CodePudding user response:

Good first try but there is a much easier way of doing this. replaceAll should do the trick

String removeVolwels(String s){
  return s.replaceAll(RegExp('[aeiou]'),'');
}

https://api.flutter.dev/flutter/dart-core/String/replaceAll.html

To make your code work you should change the || to &&

String removeVowel( str) {
  var toReturn = "";
  for (var i = 0; i < str.length; i  ) {

    var temp = str.substring(i,i 1);

    if (temp != 'a' && temp != 'e' && temp != 'i' && temp != 'o' && temp!= 'u')
    {
      toReturn = toReturn   temp;
    }
  }
  return toReturn;
}
  • Related