You can use Contains in OCL to test whether one string includes a specified substring; use it when defining text-based conditions in a ViewModel.
Syntax
StringExpression.Contains(SubstringExpression)Contains returns a Boolean value:
| Expression | Result |
|---|---|
'Hello'.Contains('e')
|
true, because e occurs in Hello.
|
'Hello'.Contains('x')
|
false, because x does not occur in Hello.
|
'Hello'.Contains('Hell')
|
true, because Hell is a substring of Hello.
|
Use dot notation for strings
Call Contains on a string with dot notation:
'Hello'.Contains('e')Do not use the collection-style arrow form for a string:
'Hello'->Contains('e')Although this expression returns true in this example, it is not subscribed as you may expect. It can keep returning its initial value when the underlying string changes. Use . for string Contains expressions so that the expression is subscribed correctly.
Example: test a ViewModel value
If a ViewModel has a string value named Name, test whether it includes son like this:
Name.Contains('son')This expression evaluates to true for a value such as 'Andersson' and false for 'Maria'.
You can use the Boolean result together with the operators described in OCL Boolean Operators when you need a larger condition.
