Logo

Programming-Idioms

History of Idiom 296 > diff from v14 to v15

Edit summary for version 15 by programming-idioms.org:
[Dart] +DocURL

Version 14

2022-03-28, 23:21:48

Version 15

2022-03-29, 20:43:00

Idiom #296 Replace last occurrence of substring

Assign to x2 the value of string x with the last occurrence of y replaced by z.
If y is not contained in x, then x2 has the same value as x.

Idiom #296 Replace last occurrence of substring

Assign to x2 the value of string x with the last occurrence of y replaced by z.
If y is not contained in x, then x2 has the same value as x.

Variables
x2,x,y,z
Variables
x2,x,y,z
Extra Keywords
substring substitute
Extra Keywords
substring substitute
Code
var position = x.lastIndexOf(y).clamp(0, x.length);
var x2 = x.replaceFirst(y, z, position);
Code
var position = x.lastIndexOf(y).clamp(0, x.length);
var x2 = x.replaceFirst(y, z, position);
Doc URL
https://api.dart.dev/stable/2.16.1/dart-core/String/replaceFirst.html
Demo URL
https://dartpad.dev/?id=daee3b76bc42ab82109b0b1b95363e49
Demo URL
https://dartpad.dev/?id=daee3b76bc42ab82109b0b1b95363e49