skip to Main Content

I’m using the code below to replace the letters "êm" with "em", but it’s not working.

str = str.replaceAll('êm', 'em');

I want to use the code to remove the accents only from some specific words such as: "crêem", "vêem", "dêem". I can’t do a general code, because other words like "têm" need to be kept with the accent.

2

Answers


  1. Just replace the accent ê with e:

      String s = 'vêem';
      
      s = s.replaceAll('ê' , 'e');
      
      print(s);
    

    Which prints:

    veem
    
    Login or Signup to reply.
  2. You can achieve this by below code:

    void main() {
      String str = "Eles crêem que vêem e dêem mas têm que aprender.";
      
      // Use a map to define the replacements
      Map<String, String> replacements = {
        'crêem': 'creem',
        'vêem': 'veem',
        'dêem': 'deem'
      };
      
      // Replace only the specified words
      str = str.replaceAllMapped(RegExp(r'b(?:crêem|vêem|dêem)b'), (match) {
        return replacements[match.group(0)]!;
      });
      
      print(str); // Eles creem que veem e deem mas têm que aprender.
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search