Experience Salesforce
String Data Type in APEX
What You’ll Learn
- Why Do We Use String in Apex?
- String Methods in Apex
Topics
- Apex Data Types
- Integer Data Type in APEX
- Floating Point Data Type in APEX
- String Data Type in APEX
- Date Datatypes in APEX
- Time and DateTime Data Types in APEX
- Boolean Datatype in Salesforce
- ID & Blob Datatypes in APEX
- What is the Rule of Conversions in Apex?
- Enums in APEX
- sObjects in Salesforce
- Generic sObjects in Salesforce
- Collections in APEX
- Lists in APEX
- List Array Notation in APEX
- Lists Initialization in APEX
- Nested Lists in APEX
- Sets in APEX
- Sets Initialization And Methods
- Maps in APEX
- Map Initialization Methods in APEX
- Operators in Apex and Their Types
- Shorthand Operator
- Equality Operator
- Relational Operators(<,>,<=,>=)
A string is any set of characters surrounded by single quotes. It can be null or empty and includes leading and trailing spaces. Strings are among the most commonly used data types in any programming language, and Apex is no exception. The String datatype in Apex represents a sequence of characters.
Why Do We Use String in Apex?
The String datatype is extensively utilized in Apex because of its adaptability and essential function in managing and modifying textual data. Here are a few justifications for why strings are crucial and used frequently in Apex:
1. Data Manipulation and Storage
Strings store textual data such as names, addresses, and descriptions. Using the + operator, you can combine multiple strings or add text to existing strings.
2. Interacting with Salesforce Records
Many Salesforce object fields, such as Name, Email, and Description, are of the String type. Constructing and processing SOQL (Salesforce Object Query Language) queries often involves string manipulation.
3. User Input and Output
Strings are frequently used to capture and display user input in Visualforce pages or Lightning components. Similarly, string manipulation is typically used to display messages, labels, and prompts to users.
4. Integration with External Systems
Interacting with external systems through REST or SOAP APIs often requires constructing and parsing JSON or XML payloads, which are strings. Building and parsing URLs for API requests also involves string operations.
5. Utility and Helper Methods
Implementing validation logic for text inputs, such as checking, if an email address is well-formed, often relies on string methods. String manipulation is used to format strings for output, such as converting dates to readable formats or formatting numbers.
6. Search and Replace Operations
Methods like contains(), indexOf(), and matches() are used to search for specific patterns or substrings. Meanwhile, methods like replace() allow for the modification of parts of a string, which is helpful in data cleaning and processing.
7. Conditional Logic
Strings can be compared using equals(), equalsIgnoreCase(), and relational operators for sorting or decision-making. They can also be used in switch statements to execute different code paths based on the value of a string variable.
8. Logging and Debugging
When debugging, information is often logged to the console or system log by concatenating strings to form readable messages. Constructing detailed error messages typically involves string operations.
Declaring a string variable in APEX
String str1 = ‘Hello World’;
String Methods in Apex
Apex provides a variety of built-in methods for manipulating strings. Here are some commonly used methods
- length(): Returns the number of characters in the string.
Integer length = str1.length(); // Output: 13
- substring(startIndex, endIndex): Returns a new string that is a substring of this string.
String subStr = str1.substring(0, 5); // Output: 'Hello'
- toLowerCase() and toUpperCase(): Converts all characters in the string to lower case or upper case.
String lowerStr = str1.toLowerCase(); // Output: 'hello, world!'
String upperStr = str1.toUpperCase(); // Output: 'HELLO, WORLD!'
- trim(): Removes leading and trailing whitespace from the string.
String trimmedStr = ' Apex '.trim(); // Output: 'Apex'
- replace(oldChar, newChar): Replaces each occurrence of a specified character with a new character.
String replacedStr = str1.replace('World', 'Apex'); // Output: 'Hello, Apex!'
- contains(substring): Checks if the string contains the specified sequence of characters.
Boolean contains = str1.contains('World'); // Output: true
- indexOf(substring): Returns the index of the first occurrence of the specified substring.
Integer index = str1.indexOf('World'); // Output: 7
- split(regex): Splits the string around matches of the given regular expression.
List<String> parts = str1.split(','); // Output: {'Hello', ' World!'}
- startsWith(prefix): Checks if the string starts with the specified prefix.
Boolean startsWithHello = str1.startsWith('Hello'); // Output: true
- endsWith(suffix): Checks if the string ends with the specified suffix.
Boolean endsWithWorld = str1.endsWith('World!'); // Output: true
Next Topic
Need Extra Support? Our FREE study materials have got you covered.
Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.