Sunday, April 5, 2015

Convert Complex String into Normal Format in Apex

// An array contains lst of Strings 
// Name, Title/Role, Company Name 
List<String> lstIntroductionWords = new List<String>{'Marc Benioff', 'CEO', 'Salesfore.com'};

// A string contains senetence template for the introduction
String templateIntroduction = 'My name is {0}. I am The {1} of {2}.';    

// A string contains sentence with complete words populated in a template sentence
String completeIntroduction = String.format(templateIntroduction, lstIntroductionWords);    

// See the complete sentence for the introduction
System.Debug('>>Introduction<<'+completeIntroduction);

Result should be as below sentence
>>Introduction<< My name is Marc Benioff. I am The CEO of Salesfore.com.