Split Delimited Value in AL - Business Central

 A List can only be declared with simple types (Byte, Boolean, Char, Code, Date, DateFormula, DateTime, Decimal, Text, Time, etc.) and does not support holding instantiated records.

Lists are an efficient way to create and manage unbounded data structures with many practical uses.

Only a few days pass when I do not find a reason to use lists. One of my recent uses for a List was to store and process each unique value found in a delimited text value. For this, I split the delimited text into separate values and then copied them to a new list removing duplicates.

In this example, a value delimited by a comma (‘,’) or pipe (‘|’) is split into a list of unique values.


procedure SplitValues() var UniqueValues: List of [Text]; DelimitedText: Text; value: Text; begin DelimitedText := '12,34,56,24|12,56,89,56,23|12,34,22,34'; UniqueValues := SplitUniqueValues(DelimitedText); foreach value in UniqueValues do Message(value); end; local procedure SplitUniqueValues(DelimitedText: Text): List of [Text] var UniqueValues: List of [Text]; Values: List of [Text]; Delimiters: Text; value: Text; begin Delimiters := ', |'; Values := DelimitedText.Split(Delimiters.Split(' ')); foreach value in Values do if not UniqueValues.Contains(value) then UniqueValues.Add(value); exit(UniqueValues); end;

No comments:

Post a Comment