FIND Function

Material prepared from `M Programming Book`_ (Page 64) [WALTERS1997] [1] and `GT.M Programmer’s Guide`_


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:


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:


  • 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")
GTM>SET X=$FIND("I am a cat","a c")

GTM>WRITE $FIND("I can plant a tree on the moon","e", 20)

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:


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:


Will break on you.


$FIND page at GT.M Programmer’s Guide

[1]Book was unavailable at specified page at the time of writing.

Table Of Contents

Previous topic

EXTRACT Function

Next topic

FNUMBER Function

This Page

inserted by FC2 system