Logo

Programming-Idioms

History of Idiom 296 > diff from v13 to v14

Edit summary for version 14 by Manu:
New Dart implementation by user [Manu]

Version 13

2022-03-18, 13:35:48

Version 14

2022-03-28, 23:21:48

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);
Demo URL
https://dartpad.dev/?id=daee3b76bc42ab82109b0b1b95363e49