Home > other >  Flutter: Replace particular string after '.jpg'
Flutter: Replace particular string after '.jpg'

Time:07-31

I have strings of jpg name

a = '123a.jpg'
b = '456asa.jpg'
c = '789pa.jpg'

Is there a way to replace the final a before .jpg with b?

new_a = '123b.jpg'
new_b = '456asb.jpg'
new_c = '789pb.jpg'

CodePudding user response:

Using a small work arround:

a = a.split('').reversed.join().replaceFirst('a', 'b').split('').reversed.join();

CodePudding user response:

Try

String newa = a.replace("a.", "b.");

Edit. You can also do

newa = a.replaceAll("a$","b");

Here $ means last index

  • Related