Recently, I managed an email marketing campaign in our Salesforce org, and the marketing team uploaded email subject lines to Salesforce. However, to ensure consistency and avoid any issues with storage limits or formatting when sending emails, I need to truncate subject lines longer than 80 characters.
In this Salesforce tutorial, you will learn how to truncate a string in Salesforce Apex. I will also explain how to truncate the string to a specific length in Salesforce Apex.
Syntax to Truncate String in Salesforce Apex
In Salesforce Apex, the syntax for truncating a string is to use the substring() method. This method extracts a portion of a string to truncate it to a specific length.
String truncatedString = String.substring(0, desiredLength);In the above syntax, String is the string to truncate. The desiredLength is the number of characters you want to keep, starting from the left (index 0).
Truncate String in Salesforce Apex
We will use the class method to truncate a string in Salesforce Apex. To do so, navigate to the developer console and follow the steps below.
- Create an Apex class in the developer console; select File > New > Apex class.
- Enter the name of the new class and click OK. Then, enter the code below in the developer console and save it.
public class TruncateString {
public static String truncateString(String input, Integer maxLength) {
if (input != null && input.length() > maxLength) {
return input.substring(0, maxLength);
}
return input;
}
}- If there are no problems or errors in the code, then launch the anonymous window with the shortcut key “Ctrl+E”. Enter the code below in the anonymous window and click the Execute button.
String inputString = 'Truncate this String value in debug';
Integer maxLength = 20;
String result = StringTruncate.truncateString(inputString, maxLength);
System.debug('Truncated String: ' + result);- In the console log window, select the checkbox to debug only; there, you will see the truncated value of the string in the debug.

We can truncate a string in Salesforce Apex by following the above steps.
Truncate a String to a Specific Length in Salesforce Apex
In Salesforce Apex, we can also truncate a string to a specific length by applying conditionals. For example, the string should only be truncated if it is longer than 120 characters.
Now, follow the steps below to truncate a string to a specific length in Salesforce Apex.
- In the Salesforce developer console, launch the anonymous window with the shortcut keys “Ctrl+E.” Enter the code below in the anonymous window, then click the Execute button.
String sizeString = 'Salesforce is a powerful cloud-based customer relationship management (CRM) platform designed to help businesses streamline their sales, marketing, and customer service processes.';
if(sizeString.length() > 120){
// Use substring to retain only the first 120 characters
sizeString = sizeString.substring(0, 120);
}
System.debug(sizeString);- In the above code, I entered a string value of over 120 characters. In the debug, the string will be truncated to 120 characters.
- In the Console log window, select the checkbox to debug only, and then you will see that the string is truncated to 120 characters.

This way, we can truncate a String in Salesforce Apex to a specific length with the help of the above code.
Conclusion
In this Salesforce tutorial, we have a method to truncate a String in Salesforce Apex using the substring() method. We also learned about the syntax that is used to truncate strings in Salesforce Apex. Along with that, we also learned about a truncate method, through which we can truncate a string to a specific length in Salesforce Apex.
You may also like to read.
- Check if a String is Null, Empty or Blank in Salesforce Apex
- Split String in Salesforce Apex
- Convert String To Lowercase And Uppercase In Salesforce Apex
- Convert String to Blob in Salesforce Apex
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.