FIND Function¶
Material prepared from M Programming Book (Page 64) [WALTERS1997] [1] and GT.M Programmer’s Guide
$FIND¶
The $FIND function is a special function built into M to allow you to search strings for certain substrings. You call call $FIND in one of three ways:
$FIND
$F
And it accepts any sort of capitalization: $Find, $find, $f, $FInd, $FiNd, are all syntactically correct, though they may not abide by the style guidelines of your project.
The $FIND function takes two or three arguments as input:
$FIND("Hello","el")
$FIND("Hello","el",2)
$FIND(STRING1,STRING2)
$FIND(STRING1,STRING2,INT)
- The first argument is the string which is searched through.
- The second argument is the substring to find inside of the first argument.
- The third argument is an integer which specifies a starting position for the search.
It should be noted that as a special function, you cannot use $FIND as a command, for instance:
$FIND("PIE", "cake")
is not a valid line of M code, you can only use it in conjunction with a line command, like in the following examples:
GTM>WRITE $FIND("I am a pidgeon","p")
9
GTM>SET X=$FIND("I am a cat","a c")
GTM>WRITE X
9
GTM>WRITE $FIND("I can plant a tree on the moon","e", 20)
26
The $FIND function will return one of three things: * If it finds a substring, it returns the index after the last character in the substring, index+1. * If it does not find the substring, it returns zero. Examples:
GTM>WRITE $FIND("I","I")
2
GTM>WRITE $FIND("I","A")
0
It is important to note that M, unlike C, includes whitespace in it’s syntax, so that you cannot have a space after the comma seperating your arguments:
GTM>WRITE $FIND("I", "A")
Will break on you.
References¶
$FIND page at GT.M Programmer’s Guide
[1] | Book was unavailable at specified page at the time of writing. |