Operator Type:
Operator Scope of Action:
Operator Purpose:
Data Type Returned:
Operator First Added:
Operator in Current Baseline:
Operator Last Altered:
Operator Has Optional Arguments:
Function [other Function type actions]
Item [operators of similar scope]
Stream parsing [other Stream parsing operators]
boolean test
v9.1.0
Baseline
As at baseline
String.try{ action(s) }[.thenTry{ action(s) }]
If the first test action fails, they restore the string and allow you to process it a different way. The current success/failure state can be tested using the .failed() operator.
Saves the value of an attribute (the steam source) and attempts an action. If the action fails because one of its operators fails or the fail() operator is performed, the original value of String stream source is restored.
A .try{} may be followed by one or more .thenTry{} clauses. If the original .try{} succeeds, all subsequent .thenTry{} clauses are ignored. If the original .try{} fails, then each .thenTry{} is attempted in turn. Once a .thenTry{} clause succeeds, subsequent .thenTry{} clauses are ignored.
For example:
$MyString.try{
$MyString=$MyString.skipToWord("To:").captureWord;
}.thenTry{
fail(); //signal an explicit failure and return a 'false' value to an enclosing expression
};
looks for strings such as "To:John Doe"
If the string "To:" is not found, $MyString will be unchanged. If found, $MyString's value is set to the word that follows, up to the next whitespace character or the end of the string, i.e. "John".
See also—notes linking to here: