Convert string to camelCase for one underscore
public static String toCamelCase(String input) { Pattern p = Pattern.compile('_([a-zA-Z])'); Matcher m = p.matcher( input ); fflib_StringBuilder sb = new fflib_StringBuilder(); while (m.find()) { sb.add(m.replaceAll(m.group(1).toUpperCase())); } return sb.toString().uncapitalize(); }
Convert string to camelCase for multiple underscore
public static String toCamelCase(String input){ fflib_StringBuilder sb = new fflib_StringBuilder(); boolean capitalizeNext = false; for (String c:input.split('')) { if (c == '_') { capitalizeNext = true; } else { if (capitalizeNext) { sb.add(c.toUpperCase()); capitalizeNext = false; } else { sb.add(c.toLowerCase()); } } } return sb.toString(); }
To Camel Case Test Class
@isTest static void testToCamelCase(){ Test.startTest(); System.assertEquals(BrightPlan_Rest_Util.toCamelCase('loan_id'), 'loanId'); System.assertEquals(BrightPlan_Rest_Util.toCamelCase('Camel_case_This_word'), 'camelCaseThisWord'); System.assertEquals(BrightPlan_Rest_Util.toCamelCase('this_IS_A_TEst_for_CAMEL_Case'), 'thisIsATestForCamelCase'); Test.stopTest(); }