You can locate text in a textual region, just as most word processors locate specific strings in a file, with the find action statement.
If a starting point is not defined with the from column line statement, the find statement begins searching at the current position of the text cursor, and moves the text cursor to the first character in the first occurrence of the specified string.
Note: The find action statement conducts a case-sensitive search.
In the example:
response to ...
find in NewText "Page Two"
if the text cursor starts searching at a position after the string "Page Two", the string will not be found.
This statement must be used in conjunction with the found built-in function. (See Using the found Action Inquiry Built-in Function.) If the string is not found, the found built-in function is set to false and the text cursor does not move. For example, if the textual region SearchRegion contains the following:
when the following response is executed, and the text cursor is at column 1, line 1 of SearchRegion:
response to FindIt
find in SearchRegion "me"
the text cursor moves to the "m" in "me", and the following built-in functions are set as shown:
Function |
Value |
found |
true |
xcursor |
6 |
ycursor |
2 |
But if the following response is executed:
response to FindIt
find in SearchRegion "her"
the value of found is set to false, and the text cursor does not move.
If found returns true, you can use the xcursor and ycursor
built-in functions to find out the exact column and line position of the first character of the located string. For example:
# Find all occurrences of SearchString in colored textual
# region Document. Highlight each occurrence, using
# black on a red background.
add to Document
move to column 1 line 1
find in Document SearchString
while (found) loop
copy xcursor of Document to ColumnNo
copy ycursor of Document to LineNo
make Document segment ColumnNo LineNo
thru ((ColumnNo + length of SearchString) -1) LineNo
foreground black
background red
add to Document
move by 1 columns 0 lines
find in Document SearchString
end loop
If the cursor is already on the first character of the searched string when the find action statement is performed, the built-in function found returns true, but the text cursor does not move. Thus, simply checking for a change in the values of xcursor and ycursor after the find has been executed is not a reliable way to determine whether or not the string was found.
If you want to search for multiple occurrences of a string, you should move the cursor to the right by one character after each successful find; otherwise, an infinite loop results. This is shown in the above example with the lines:
add to Document
move by 1 columns 0 lines
You can specify a location, other than the current cursor position, to begin the search, by specifying from and the column and line numbers of the location.
For example:
response to ...
find in NewText from column 1 line 30 "Table"
finds the first occurrence of the string "Table" starting from column 1, line 30, and moves the text cursor to the "T" in "Table". If "Table" is not found, the cursor does not move.