Home > Software engineering >  Flutter how to change string alphabet by index
Flutter how to change string alphabet by index

Time:02-02

I have a string

String word = "000000";

What i need to do is change the 0 by index like

word.replaceAt(1, 3) so it will be 300000

I have search but all are just with replaceWith i want to replace by index

CodePudding user response:

Try this:

extension Replacer on String{
  String replaceAt(int index, String replacement){
    return "${this.substring(0, index)}$replacement${this.substring(index   replacement.length)}";
  }
}

And use it like:

main() {
  String word = "000000";
  word = word.replaceAt(0, "3");
  print(word); // 300000
}
  •  Tags:  
  • Related