[SalesForce] Apex String Split with single quotes

I have a String array as follows

String [] toAddress = new String [] {'abc@example.com','def@example.com','xyz@example.com'};

I would like to get the output as

'abc@example.com','def@example.com','xyz@example.com'.

Could you please give some direction on how to achieve this?

Best Answer

You can use String#join to get the output you want:

String result = '\''+String.join(toAddress,'\',\'')+'\'';

This will result in a single string with quoted addresses.