Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trigger on Account (after update) {
- String billingCity = Account.billingCity;
- billingCity = toTitleCase(billingCity);
- }
- //converts a given string to Title Case where the
- //first letter of every word is capitalised and the rest are small
- public String toTitleCase(String billingCity) {
- System.debug('billingCity ' + billingCity);
- String titlePhrase = '';
- //a set of words that should always be in lower case when in Title Case
- Set<String> forceLower = new Set<String>{'of', 'the', 'for', 'and', 'a', 'to', 'at' ,'an', 'but', 'if', 'or', 'nor'};
- if(billingCity != null && billingCity.length() > 0){
- String[] splitPhrase = billingCity.trim().split(' ');
- for(integer i = 0; i < splitPhrase.size(); i++){
- if(!forceLower.contains(splitPhrase[i].toLowerCase()) || i == 0 || i == (splitPhrase.size()-1) ) {
- titlePhrase += (splitPhrase[i].substring(0,1).toUpperCase())+(splitPhrase[i].substring(1).toLowerCase())+' ';
- } else {
- titlePhrase += splitPhrase[i].toLowerCase()+' ';
- }
- }
- }
- return titlePhrase.trim();
- }
Add Comment
Please, Sign In to add comment