fbpx

Free Salesforce Developers Tutorials >

Chapter 3 - Apex Datatypes and Operators >

String Data Type in APEX

String Data Type in APEX

What You’ll Learn


S2 Labs

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’;

Start Your Salesforce Developer Journey CTA

String Methods in Apex

Apex provides a variety of built-in methods for manipulating strings. Here are some commonly used methods

  1. length(): Returns the number of characters in the string.
Integer length = str1.length(); // Output: 13
  1. substring(startIndex, endIndex): Returns a new string that is a substring of this string.
String subStr = str1.substring(0, 5); // Output: 'Hello'
  1. 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!'
  1. trim(): Removes leading and trailing whitespace from the string.
String trimmedStr = ' Apex '.trim(); // Output: 'Apex'
  1. replace(oldChar, newChar): Replaces each occurrence of a specified character with a new character.
String replacedStr = str1.replace('World', 'Apex'); // Output: 'Hello, Apex!'
  1. contains(substring): Checks if the string contains the specified sequence of characters.
Boolean contains = str1.contains('World'); // Output: true
  1. indexOf(substring): Returns the index of the first occurrence of the specified substring.
Integer index = str1.indexOf('World'); // Output: 7
  1. split(regex): Splits the string around matches of the given regular expression.
List<String> parts = str1.split(','); // Output: {'Hello', ' World!'}
  1. startsWith(prefix): Checks if the string starts with the specified prefix.
Boolean startsWithHello = str1.startsWith('Hello'); // Output: true
  1. endsWith(suffix): Checks if the string ends with the specified suffix.
Boolean endsWithWorld = str1.endsWith('World!'); // Output: true

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Download Now

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners . But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

Your email address will not be published. Required fields are marked *