Redirected from Guide for writing business rules with OCL/Available methods for OCL base types/.lastIndexOf().lastIndexOf()
Description of the method
|Description:||.lastIndexOf() method returns the last position of string or character from a string. The string or character to be searched for is given as parameter. If nothign is found "-1" will be returned. |
Check .indexOf() if you are trying to find the first occurrence of string or character.
|OCL:||self.Id.lastIndexOf('content') = 9|
|Description:||The example checks if last string 'content' is found starting from the position 1 of the value of the <Id> element. First position of a string is 0.|
The XML snippet below would pass this check as the last occurrence of string "content" starts from index 9. First occurrence is starting from index 1.
The other snippet below however would not pass this check as the string "content" starts from index 0.
|OCL:||self.Id.lastIndexOf('a') = -1|
|Description:||The rule here checks if character a is found from the value of element <Id>. The rule passes if character a is not found as then the lastIndexOf method returns value -1.|
The XML snippet below would pass this check as the string "value" starts from index 1 (first index is 0).
The other snippet below however would not pass this check as the string "value" starts from index 0.
Attached you can find example schema and files which you can use for testing.