You can use asSeparatedList in an OCL expression to turn a collection into one display-ready string, with a delimiter that you choose.
Syntax
<collection>->asSeparatedList(<separator>)
| Part | Description |
|---|---|
<collection>
|
The collection whose elements you want to join. Create a collection of strings with collection expressions, such as Set{...}.
|
<separator>
|
The string inserted between adjacent elements. Examples include ', ', '_', ' ', and .
|
Each element is converted to text with asString before the operator joins the elements.
Join strings with a chosen separator
Use the separator argument when commas are not the required format. For example, this expression uses an underscore:
Set{'x','y','x'}->asSeparatedList('_')
A Set contains no duplicates, so the repeated 'x' occurs only once in the resulting text. The result contains x and y, separated by an underscore:
x_y
Do not use a Set when the order of the joined values is significant. A set is for unique values; choose a collection type that preserves the order you need before applying asSeparatedList.
Build a display label from optional values
You can first build a collection whose contents depend on the model, then join its strings. This example produces a comma-and-space separated label from the material, color, and finish values that are available:
Sequence{
self.ValidMaterials->notEmpty.caseTrueFalse('Materials', String.nullValue),
self.ValidColors->notEmpty.caseTrueFalse('Colors', String.nullValue),
self.ValidFinish->notEmpty.caseTrueFalse('Finish', String.nullValue)
}->asSeparatedList(', ')
For example, when the material and finish conditions are true but the color condition is false, the intended display text is:
Materials, Finish
See Documentation:Collection of strings for more examples of constructing collections in OCL and EAL expressions.
Use no separator
Pass an empty string when you want to concatenate the elements without characters between them. For example:
Set{'A','B','C'}->asSeparatedList('')
This produces text with the elements directly adjacent, such as ABC.
Related string operations
Use asCommaList when a comma is always the required separator. To convert separated text back to a collection, use split; its separator must be supplied as a character array. Use regExpSplit when the split positions are defined by a regular expression.
