In Microsoft Dynamics 365 Business Central, you split a string using the built-in Split method on a Text or Code variable, which returns a List of [Text] containing the substrings. For example, Text.Split(',') breaks a comma-separated string into a list. This article explains the method, its variations, common pitfalls, and practical examples.
What Does Splitting a String Mean in Business Central?
Splitting a string means dividing a single text value into multiple parts based on a delimiter (like a comma, semicolon, or space). In AL (the programming language for Business Central), the Split method is part of the Text data type. It returns a List of [Text], which you can then iterate over or access by index.
The basic syntax is:
var
InputText: Text;
Delimiter: Text;
ResultList: List of [Text];
begin
InputText := 'apple,banana,cherry';
Delimiter := ',';
ResultList := InputText.Split(Delimiter);
end;
The Split method is case-sensitive and treats the delimiter as a literal string, not a regular expression. If the delimiter appears multiple times, each occurrence creates a new element. If the delimiter is empty, the method returns a list with a single element containing the entire input string.
How the Split Method Works Under the Hood
The Split method is defined on the Text and Code data types. It takes one parameter: the delimiter (a string). It returns a List of [Text]. The method scans the input string from left to right, and each time it finds the delimiter, it extracts the substring before it and adds it to the list. After the last delimiter, the remaining substring is added as the final element.
Important details:
- If the input string is empty, the returned list is empty.
- If the delimiter is not found, the list contains one element: the entire input string.
- Consecutive delimiters (e.g., “a,,b”) produce empty strings as elements.
- The method does not trim spaces; you must use
Trimon each element if needed.
Example with consecutive delimiters:
InputText := 'a,,b';
ResultList := InputText.Split(',');
// ResultList[0] = 'a', ResultList[1] = '', ResultList[2] = 'b'
Practical Examples of Splitting Strings
Here are common scenarios where you might split a string in Business Central:
- Parsing CSV lines: Reading a file where each line has comma-separated values.
- Extracting email addresses: Splitting a list of emails separated by semicolons.
- Processing multi-value fields: Some fields store multiple values with a delimiter; splitting helps you handle each individually.
Example: Parse a line from a CSV file
var
Line: Text;
Fields: List of [Text];
FieldValue: Text;
begin
Line := 'John,Doe,35,New York';
Fields := Line.Split(',');
foreach FieldValue in Fields do
Message(FieldValue); // Shows each field
end;
Example: Split a semicolon-separated list of email addresses and process each
var
EmailList: Text;
Emails: List of [Text];
Email: Text;
begin
EmailList := 'a@example.com;b@example.com;c@example.com';
Emails := EmailList.Split(';');
foreach Email in Emails do
// Do something with each email
end;
Choosing the Right Delimiter and Handling Edge Cases
Selecting the delimiter depends on your data. Common delimiters include comma, semicolon, pipe (|), tab (\t), and newline (\n). Note that the Split method takes a string, so you can pass a multi-character delimiter like ', ' (comma followed by space) if your data uses that pattern.
Edge cases to consider:
- Empty strings: If the input is empty, the list is empty. If you expect one empty element, handle it manually.
- Delimiter at the start or end:
',apple'gives an empty first element;'apple,'gives an empty last element. - Delimiter not present: The list has one element, the whole string.
- Case sensitivity: The delimiter is matched exactly. If your data uses mixed case, consider converting to lower case first.
If you need to split on multiple different delimiters (e.g., both comma and semicolon), you can first replace one with the other, then split. For example:
InputText := 'a,b;c';
InputText := InputText.Replace(';', ',');
ResultList := InputText.Split(','); // Now gives 'a', 'b', 'c'
Common Problems and How to Avoid Them
Several issues frequently arise when splitting strings in Business Central:
- Forgetting that Split returns a List, not an array: You cannot use
ResultList[0]directly; you must useResultList.Get(0)or iterate withforeach. - Empty elements causing errors: If you expect a certain number of fields, an empty element may break your logic. Check for empty strings before processing.
- Delimiter inside quoted fields: If your CSV has quoted fields containing commas, the simple split will break. You need a more robust parser that respects quotes.
- Performance with large strings: Splitting a very large string (e.g., a whole file) can be memory-intensive. Consider streaming or processing line by line.
Example of accessing an element by index:
var
ResultList: List of [Text];
FirstElement: Text;
begin
ResultList := 'a,b,c'.Split(',');
if ResultList.Count > 0 then
FirstElement := ResultList.Get(0); // 'a'
end;
Alternatives to Split: Other String Functions
Sometimes splitting is not the best approach. Business Central offers other string functions that might suit your needs:
CopyStr– extracts a substring by position and length.FindStr– finds the position of a substring within a string.DelChr– removes characters from a string.InsStr– inserts a string at a specific position.SelectStr– extracts a substring between two delimiters (but only the first occurrence).
For example, if you need only the second field of a comma-separated string, you could use SelectStr instead of splitting the whole string:
var
InputText: Text;
SecondField: Text;
begin
InputText := 'apple,banana,cherry';
// SelectStr(1, InputText, ',') returns 'banana' (the second field)
SecondField := SelectStr(1, InputText, ',');
end;
However, SelectStr only works for a single occurrence; for multiple fields, splitting is more straightforward.
Frequently Asked Questions
Can I split a string by multiple characters at once?
No, the Split method only accepts a single delimiter string. If you need to split on multiple different delimiters, you must first normalize the string by replacing all delimiters with a common one, then split. For example, replace semicolons with commas, then split on commas.
How do I convert the result list to an array?
You can convert a List of [Text] to an array using the ToArray method. For example: ResultArray := ResultList.ToArray(); This gives you a fixed-size array that you can index directly.
Does Split work with Code fields?
Yes, the Split method is also available on the Code data type. However, Code fields are case-insensitive and have a maximum length of 50 characters, so the behavior is the same but with those constraints.
What if my string contains a delimiter that is also part of the data?
If your data contains the delimiter within the values (e.g., a comma inside a quoted field), the simple Split will incorrectly split. You need a custom parser that respects quotes. One common approach is to iterate through the string character by character and track whether you are inside quotes.
Is there a performance difference between Split and other methods?
Splitting a large string can be slower than using SelectStr if you only need a few fields, because Split creates a list of all substrings. If you only need one field, consider using SelectStr or CopyStr with FindStr to avoid the overhead.
Can I split a string on a newline character?
Yes, you can pass '\n' as the delimiter. However, note that line endings can be \r\n on Windows. In that case, you might want to replace '\r\n' with '\n' first, or split on '\r\n' directly if your data is consistent.