Flutter: Replace particular string after '.jpg'

Viewed 42

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'
2 Answers

Using a small work arround:

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

Try

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

Edit. You can also do

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

Here $ means last index

Related