Use asCommaList in an OCL or EAL expression when you need to turn a collection of values into one comma-separated string, for example for a label or display field.
Join a collection with commas
asCommaList() converts each element in a collection to text and joins the elements with a comma between them. The result is one string.
Set{'x', 'y'}->asCommaList()
The result is:
'x,y'
A Set contains no duplicate values. Therefore, this expression contains only 'x' and 'y'; the repeated 'x' is not included twice:
Set{'x', 'y', 'x'}->asCommaList()
The result is:
'x,y'
Build a list from conditional values
You can create a collection of strings from expressions and then join it. This is useful when the number of displayed values depends on the data.
Sequence{
self.ValidMaterials->notEmpty.caseTrueFalse('Materials', String.nullValue),
self.ValidColors->notEmpty.caseTrueFalse('Colors', String.nullValue),
self.ValidFinish->notEmpty.caseTrueFalse('Finish', String.nullValue)
}->asCommaList()
For example, if only ValidMaterials and ValidFinish are non-empty, the resulting string is:
'Materials,Finish'
Choose another separator
Use asSeparatedList when commas are not appropriate. For example, use an underscore to create x_y instead of x,y.
Split a comma-separated string
To turn a comma-separated string back into a collection of strings, split on the comma and trim each value. Trimming removes spaces that occur after commas.
'Materials, Colors, Finish'.split(',')->collect(s | s.trim)
This produces a collection containing 'Materials', 'Colors', and 'Finish', without leading spaces.
When the separator may vary, or when you need to split on a pattern such as commas, semicolons, or whitespace, use regExpSplit instead.
