[SalesForce] How to split a string by comma and single quotation mark in apex

Following is a string:

 'First Name','Last Name','Account Name','Email','Title','Description'

and I want it to split with ",'" (comma and single quotation mark).

Best Answer

Try with this

First remove all single quotes and then split by comma

String str = '\'First Name\',\'Last Name\',\'Account Name\',\'Email\',\'Title\',\'Description\'';
System.debug('===str==='+str);
for(String strFinal: str.replace('\'', '').split(','))
{
    System.debug('===Final val==='+strFinal);
}

enter image description here

Another option

If we are sure single quotes will be start and end of string then we can remove using string method

for(String strFinal: str.split(','))
{
    System.debug('===Final val==='+strFinal.removeStart('\'').removeEnd('\''));
}