Guest User

Untitled

a guest
Jan 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. trigger on Account (after update) {
  2.  
  3. String billingCity = Account.billingCity;
  4.  
  5. billingCity = toTitleCase(billingCity);
  6. }
  7.  
  8. //converts a given string to Title Case where the
  9. //first letter of every word is capitalised and the rest are small
  10. public String toTitleCase(String billingCity) {
  11. System.debug('billingCity ' + billingCity);
  12. String titlePhrase = '';
  13. //a set of words that should always be in lower case when in Title Case
  14. Set<String> forceLower = new Set<String>{'of', 'the', 'for', 'and', 'a', 'to', 'at' ,'an', 'but', 'if', 'or', 'nor'};
  15.  
  16. if(billingCity != null && billingCity.length() > 0){
  17. String[] splitPhrase = billingCity.trim().split(' ');
  18.  
  19. for(integer i = 0; i < splitPhrase.size(); i++){
  20. if(!forceLower.contains(splitPhrase[i].toLowerCase()) || i == 0 || i == (splitPhrase.size()-1) ) {
  21. titlePhrase += (splitPhrase[i].substring(0,1).toUpperCase())+(splitPhrase[i].substring(1).toLowerCase())+' ';
  22.  
  23. } else {
  24. titlePhrase += splitPhrase[i].toLowerCase()+' ';
  25.  
  26. }
  27. }
  28. }
  29.  
  30. return titlePhrase.trim();
  31. }
Add Comment
Please, Sign In to add comment