function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
azhar khasimazhar khasim 

Need to add & symbol before last word in the string!!!

Hello Everyone,

I had small query

Example 1 :
i have a string = Mom, Dad, Son, Daughter
i want a string = Mom, Dad, Son & Daughter

Example 2 :
i have a string = Mom, Dad, Son
i want a string = Mom, Dad & Son

Please help me to provide a solution for this.

Thanks,
Azar Khasim.
Best Answer chosen by azhar khasim
McCuboMcCubo
Hi Azar,
If your use case is to always replace the last separating comma in your string, you can use something like this:
String a = 'Mom, Dad, Son, Daughter';
String newString = a.reverse().replaceFirst(',', ' &').reverse();