Hello friends, In this post you are going to learn, how to put quotes(inverted comma) in every list element? This is very helpful for you when you have to pass list value in dynamic soql query. So without wasting time let's get started,

Add quotes in every list elements in apex

To add quotes in every elements in list we have use String.join function, that convert a list to comma separated quotes value string. See the below example:

 List<Id> accountIds=new List<Id>();
for(Account a:[SELECT Id FROM Account limit 5]){
accountIds.add(a.Id);
}
string IdsWithQuotes='(\''+String.join(accountIds,'\',\'')+'\')';
System.debug('IdsWithQuotes^^ '+IdsWithQuotes);

Output:

Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.