If you are a programmer, you must be aware of the adverse requirement of manipulating texts in your code. Moreover, performing several functions on characters is a requirement in almost every scenario. As per the coding language, texts are stored in a primitive data type known as String. The String class helps the user perform several operations and get the desired output.
Today, we are going to discuss one of the most anticipated topics in Salesforce, Apex String Class. If you are looking for a complete guide that covers all the major details, then you are at the right place. Let’s begin our journey.
What is Apex String Class in Salesforce?
Apex is capable of distinguishing between several String types like picklists, texts, long and rich text areas, and a lot more. Any class that stores characters or text values, is considered to be a String in Salesforce. As mentioned earlier, performing multiple actions on a single string is a common practice for coders. Thus, Apex provides inbuilt methods that help you change the String format by a simple call to that specific method.
All these come under the umbrella of String Class. In Apex terms, a String is anything that can store texts. Hence, these methods can be used to iterate over any string in the system.
Common Apex String Class Methods
Now, we shall take a look at some of the common methods available in the String class. Once you get habitual in using them, it will save a lot of time and effort in building the code that uses Strings.
- charAt(index) : It will result in the ASCII code of the character that is present on the index of the string.
- indexOf(‘string’) : Opposite to the previous method, this will return the starting position of the string.
- contains(‘string’) : If the string is a subset of the main string, the function will return true.
- equals(‘string’) : This returns true if both the strings are exactly the same.
- equalsIgnoreCase(‘string’) : It is similar to equals, however, doesn’t consider the case of the characters in the string.
- substring(start, end) : Just provide the start and end index, and the function will return the string within those endpoints.
- isBlank(‘string’) : As the name stands, it will return true/false depending on whether the string is empty or not.
You can clearly see that this list of methods can go on forever. It depends on you how well can you make use of the Apex String Class to improve the functionality of your code.
We have named string variables as ‘str’ in our examples which is obviously not an appreciated practice. It is advisable to use descriptive names like companyName, customerId, etc for your Strings. This will increase the overall readability of your code.
Also Read: Mastering Future Method in Salesforce
String functions are pretty useful and showcase how important the Apex String class is for the developers. It is not mandatory to memorize the mentioned methods before proceeding. You will gradually learn them as you keep on using them in the code.
We hope our ultimate guide on Salesforce Apex String Class gave you a complete overview of the methods available. Make sure to drop your doubts in the comment section below