Splitting Text in Microsoft Dynamics 365 Business Central with Text.Split
Senthil Kumar Varatharajan
Manager – D365 BC Technical Business Central at Mercurius IT| Microsoft Dynamics & SAP Solutions for better business | A safe pair of hands
In Microsoft Dynamics 365 Business Central, the Text.Split method is a powerful tool for splitting a text string into an array of substrings based on a specified delimiter. This method is particularly useful for data manipulation and extraction tasks within your Business Central applications. In this article, we'll explore the syntax and practical examples of how to use Text.Split.
Syntax
The Text.Split method is defined as follows:
Result := Text.Split(Separators: List of [Text])
Parameters
Separators: Type: List of [Text] A collection of separators that delimit the substrings in this string.To split a simple comma-separated string:
local procedure SplitStringusingCommas()
var
SourceText: Text;
Delimiter: Text;
Substrings: List of [Text];
Substring: Text;
begin
SourceText := 'apple,banana,cherry';
Delimiter := ',';
Substrings := SourceText.Split(Delimiter);
foreach Substring in Substrings do
Message(Substring);
end;
This example splits the string 'apple,banana,cherry' into the substrings 'apple', 'banana', and 'cherry'.
Splitting by Multiple Characters
To split a string using multiple characters as the delimiter:
local procedure IncludeMultiplChar()
var
SourceText: Text;
Delimiter: Text;
Substrings: List of [Text];
Substring: Text;
begin
SourceText := 'apple--banana--cherry';
Delimiter := '--';
Substrings := SourceText.Split(Delimiter);
foreach Substring in Substrings do
Message(Substring);
end;
This example splits the string 'apple--banana--cherry' into the substrings 'apple', 'banana', and 'cherry' using the delimiter --.
Practical Example: Extracting Names
Consider a scenario where you need to extract the first name and last name from a full name string:
local procedure ExtractingNames()
var
FullName: Text;
Delimiter: Text;
NameParts: List of [Text];
FirstName: Text;
LastName: Text;
begin
FullName := 'John Doe';
Delimiter := ' ';
NameParts := FullName.Split(Delimiter);
if NameParts.Count() >= 2 then begin
FirstName := NameParts.Get(1);
LastName := NameParts.Get(2);
Message('First Name: %1', FirstName);
Message('Last Name: %1', LastName);
end;
end;
This example splits the full name 'John Doe' into first name 'John' and last name 'Doe'.
Conclusion
The Text.Split method in Microsoft Dynamics 365 Business Central is a versatile function for text manipulation, enabling you to handle strings efficiently. Whether you're parsing CSV data, extracting parts of a name, or handling complex strings with multiple delimiters, Text.Split provides the functionality you need to streamline your data processing tasks.