How To Split String Method In Salesforce Apex?

5.0K    Asked by AlGerman in Salesforce , Asked on Nov 18, 2022

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.

It is used to break your apex string split. Showing an example below with a small case about it. Maybe this can help you.


Example:

Let suppose you are getting something like address(F-206, Daffodils, Magarpatta, Pune, India, 411028) in one text field and you want to store this address in different segments like house no, Building name, Street, City, Country. for this you can use split function for this by given code. 
String addressFull = 'F-206, Daffodils, Magarpatta, Pune, India, 411028';
String[] address = addressFull.split(',');
String houseNumber = address[0];
String buildingName = address[1];
String streetName = address[2];
String cityName = address[3];
String countryName = address[4];
System.debug(houseNumber);
System.debug(buildingName);
System.debug(streetName);
System.debug(cityName);
System.debug(countryName);
Output: please check debug logs after that, you will be able to see

Your Answer

Answers (2)

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!

1 Day

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);
}
8 Months

Interviews

Parent Categories