Operator $ Foundation

Substring operator (binary): searches for a substring within another character string.

Syntax
<cSubString> $ <cString>
Parameters
<cSubString>
<cSubString> is a character expression, the value of which is searched for in the character string <cString>.
<cString>
<cString> is a character expression in which a search is made for the value of the character expression <cSubString>.
Description

The substring operator is used to search a character string to determine if it contains a given substring. The operator is case sensitive. It returns the value .T. (true) only when the substring <cSubString> is contained in the string <cString>. Otherwise it returns .F. (false).

Extended functionality of the substring operator

The substring operator is not restricted to values of data type Character, but can also be applied to values of type Array and Numeric.

Array

If the right operand is a value of data type Array, the operator searches the value of the left operand in the array and returns .T. (true) if the value is found, otherwise it returns .F. (false).

The right operand can be a multi-dimensional array. The operator searches all dimensions of the array. If the left operand is an array as well, the operator scans the left operand and searches the values of all array elements in the right operand.

If the left operand is of data type Character or if it contains character strings, the right operand is searched for a character string according to a simple comparison (see "=" operator). An "exactly equal" comparison is not performed (see "==" operator). Therefore, the search result depends on the SET EXACT ON|OFF setting.

Numeric

When both operands are of data type Numeric, the $ operator tests if the individual bits set in the left operand are also set in the right operand. The result is .T. (true) when all bits set for the left operand are set in the right operand.

Examples
The substring operator ($)
// This example demonstrates the result of the substring operator 
// which searches for a substring in a character string. 

PROCEDURE Main 

   ? "James"  $ "James Bond's BMW"      // result: .T. 
   ? "james"  $ "James Bond's BMW"      // result: .F. 
   ? "Bond's" $ "James Bond's BMW"      // result: .T. 
   ? "Bond s" $ "James Bond's BMW"      // result: .F. 

RETURN 
Using the substring operator with arrays
// This example shows how the substring operator can be 
// used with values of data type Array. 

PROCEDURE Main 

   ** Searching single values in arrays 
   ** having different dimensions 
   ? 2 $ {2,4,6}                        // result: .T. 
   ? 4 $ {{1,2},{4,6}}                  // result: .T. 
   ? 6 $ { {{1,2},"A"}, {{4,6}, "B"} }  // result: .T. 

   ** Searching sets of values in an array 
   ? {1,2,4,6} $ {1,2,4,6}              // result: .T. 
   ? {1,6}     $ {1,2,4,6}              // result: .T. 

   ? {4,6} $ {{1,2},{4,6}}              // result: .T. 
   ? {1,6} $ {{1,2},{4,6}}              // result: .T. 

   ** Searching character strings in an array 
   SET EXACT OFF 

   ? "DE"  $ {"ABC","DEF"}              // result: .T. 
   ? "DEF" $ {"ABC","DEF"}              // result: .T. 
   ? "EF"  $ {"ABC","DEF"}              // result: .F. 

   SET EXACT ON 

   ? "DE"  $ {"ABC","DEF"}              // result: .F. 
   ? "DEF" $ {"ABC","DEF"}              // result: .T. 
   ? "EF"  $ {"ABC","DEF"}              // result: .F. 
RETURN 
Using the substring operator with Numbers
// This example shows how the substring operator can be 
// used with values of data type Numeric. The function Which() 
// demonstrates how to use a combination of #define constants 
// to encode different states of a program within one numeric 
// value. The only requirement is that the constants form 
// an exponential series of integer numbers. 

 #define  SWITCH_OFF    0 
 #define  SWITCH_ONE    1 
 #define  SWITCH_TWO    2 
 #define  SWITCH_THREE  4 
 #define  SWITCH_FOUR   8 

 PROCEDURE Main 
    ? 0 $ 6                             // result: .F. 
    ? 1 $ 6                             // result: .F. 
    ? 2 $ 6                             // result: .T. 
    ? 3 $ 6                             // result: .F. 
    ? 4 $ 6                             // result: .T. 
    ? 5 $ 6                             // result: .F. 

    ? Which( SWITCH_ONE   + SWITCH_FOUR ) 
    ? Which( SWITCH_ONE   + SWITCH_TWO  ) 
    ? Which( SWITCH_THREE + SWITCH_FOUR ) 
 RETURN 

 FUNCTION Which( n ) 
    LOCAL cSwitch := "" 
    
    IF SWITCH_OFF   $ n 
       RETURN     " off" 
    ENDIF 
    IF SWITCH_ONE   $ n 
       cSwitch += " one" 
    ENDIF 
    IF SWITCH_TWO   $ n 
       cSwitch += " two" 
    ENDIF 
    IF SWITCH_THREE $ n 
       cSwitch += " three" 
    ENDIF 
    IF SWITCH_FOUR  $ n 
       cSwitch += " four" 
    ENDIF 
RETURN cSwitch 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.