String Manipulation in Salesforce #1

Salesforce provide set of methods for String manipulation. This article explains usage of each function with examples.


  1. Truncate strings with ellipses.
    abbreviate(maxWidth) returns a trimmed version of the String, of the specified length and with ellipses (…) appended if the current String is longer than the specified length; otherwise, returns the original String without ellipses.

    'Test Text Field'.abbreviate(8); //Test ...
  2. Convert to title case (Capitalize first letter)
    capitalize() capitalize first letter of the string.

    'test text field'.abbreviate(8); //Test text field
  3. Pad with Spaces (front & back)
    center(size) pads string with spaces in front and back while keeping String in the center.

    'test'.center(10); //    test
  4. Pad with Text (front & back)
    center(size, paddingString) pads string with specified text in front and back while keeping String in the center.

    'test'.center(10,'#'); //###test###
  5. Remove leading and trailing spaces
    trim() removes leading and trailing white spaces from the string.

    '   test    '.trim(); //test
  6. Escape characters.
    escapeCsv() – columns in CSV string enclosed with double quotes.
    escapeHtml3(), escapeHtml4() – escapes html characters in String.
    escapeJava() – escapes characters in String with Java rules. Characters escaped include quotes and control characters, such as tab, backslash, and carriage return characters.
    escapeSingleQuotes(stringToEscape) – escapes single quotes in String with backslash.
    escapeUnicode() – escapes Unicode characters.
    escapeXml() – escapes Xml tags in String.
  7. Convert first letter to lowercase
    uncapitalize() returns the String with the first letter in lowercase.

    'Test tTest Test'.uncapitalize(); //test tTest Test
  8. Convert to uppercase
    toUpperCase() Converts all of the characters in the String to uppercase using the rules of the default (English US) locale.

    'Test'.<code>toUpperCase</code>(); //TEST
  9. Convert to lowercase
    toUpperCase() Converts all of the characters in the String to uppercase using the rules of the default (English US) locale.

    'TesT'.<code>toLowerCase</code>(); //test
  10. Remove HTML tags
    stripHtmlTags() Removes all html tags from the String.

Loss of Fractions in Divisions

This is a small coding issue which can lead to serious calculation problems. When dividing numeric Integer or Long values, the fractional portion of the result, if any, is removed before performing any implicit conversions to a Double or Decimal.

Eg –

Double = 5/3; // Expected result = 1.6666666666666667 | Actual result = 1.0

To eliminate this issue, values used for division must be converted to fractions before.

Double = 5.0/3.0; // 1.6666666666666667


Double d = Double.valueOf(5)/Double.valueOf(3); // 1.6666666666666667

Quick Salesforce Deployments with ANT Migration Tool

Apache’s Ant is a Java library and command-line tool which allows quick deployment/migration of components into a target system. Once you installed migration tool, it can retrieve and deploy into target org by entering several commands. Instructions to install ANT can be found here or here. If you have installed ANT correctly, entering ant –version in the command line will display ANT version as below. If you having trouble with installation, please comment below.

Apache Ant(TM) version 1.10.0 compiled on December 27 2016

Assuming ANT is stored in c:\ANT directory, Following windows batch script allows fast & easy deployments with single line of command. Copy script into a file named deploy.bat, and execute it to do the deployment.

@RD /S /Q "C:\ANT\retrieveUnpackaged"
cd /d C:\ANT
call ant retrieveUnpackaged
call ant deployUnpackaged