Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How To Split String Method In Salesforce Apex?

Asked by Al German Nov 18, 2022 9.2K views 3 answers
Share

About this question

Need to split a string by the first occurrence of a specific character like '-' in 'this-is-test-data '. So what I want is when I do a split it will return 'this' in zero indexes and rest in the first index.

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jan 21, 2025

If you’re working with strings in Salesforce Apex and need to split them, the String.split() method is your go-to tool. Here’s how it works and how you can use it effectively:

1. Basic Syntax of String.split()

The split() method takes a regular expression as its parameter and splits the string wherever the pattern matches.

  String myString = 'apple,banana,grape';List fruits = myString.split(',');System.debug(fruits); // Output: (apple, banana, grape)

2. Split by a Specific Character

To split a string by a single character, such as a comma, colon, or pipe (|):

  String data = 'John|Doe|Developer';List parts = data.split('\|'); // Use double backslash for special charactersSystem.debug(parts); // Output: (John, Doe, Developer)

3. Split by Multiple Delimiters

You can use a regular expression to split by multiple delimiters. For example, splitting by both commas and semicolons:

  String text = 'one,two;three,four';List result = text.split('[,;]');System.debug(result); // Output: (one, two, three, four)

4. Handling Edge Cases

If the string is empty or doesn’t contain the delimiter, the split() method will return a list with a single empty string or the original string.

  String emptyString = '';List result = emptyString.split(',');System.debug(result); // Output: ()

5. Practical Usage

  • Splitting CSV data for processing rows.
  • Parsing user inputs or logs with multiple fields.
  • Extracting values from a delimited string in integrations.

By using String.split() in Apex, you can efficiently handle strings for dynamic and scalable solutions!

Was this helpful?

Ranjana Admin JanBask Expert

Answered on May 7, 2024

In Salesforce Apex, you can split a string into substrings using the split() method. Here's how you can use it:

String myString = 'Hello,World,How,Are,You';
// Split the string using a delimiter (comma in this case)
List parts = myString.split(',');
In this example:

myString is the original string you want to split.

split(',') splits the string using the comma , as the delimiter.

The result is stored in a List called parts, where each element represents a substring.

You can replace ',' with any other character or string you want to use as a delimiter. For example, to split by space, you would use split(' ').


Keep in mind that the split() method is case-sensitive. If you want case-insensitive splitting, you might need to preprocess the string or use regular expressions with appropriate flags.// Iterate through the parts

for(String part : parts) {
    System.debug(part);
}
Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.