strLib.findStr

The system function strLib.findStr searches for the first occurrence of a substring in a string.


strLib.findStr syntax diagram

result

Numeric item that receives one of the following values (defined as type INT or the equivalent: type BIN with length 9 and no decimal places) returned by the function:

-1

Search string was not found

0

Search string was found

source

String from which a source substring is derived. Can be an item or a literal.

sourceSubstringIndex

Identifies the starting byte for the substring in source, given that the first byte in source has the index value of 1. This index can be an item defined as type INT or the following equivalent: type BIN with length 9 and no decimal places.

sourceStringLength

Identifies the number of bytes in the substring that is derived from source. This index can be an item defined as type INT or the following equivalent: type BIN with length 9 and no decimal places.

searchString

String item or literal to be searched for in the source substring. Trailing blanks or nulls are truncated from the search string before searching begins.

If searchString is found in the source substring, sourceSubstringIndex is set to indicate its location (the byte of the source where the matching substring begins). Otherwise, sourceSubstringIndex is not changed.

Definition considerations

The following values are returned in sysVar.errorCode:

8

Index less than 1 or greater than string length.

12

Length less than 1.

20

Invalid double-byte index. Index for a DBCHAR or UNICODE string points to middle of double-byte character.

24

Invalid double-byte length. Length in bytes for a DBCHAR or UNICODE string is odd (double-byte lengths must always be even).

Example

  source = "123456";
  sourceIndex = 1
  sourceLength = 6
  search = "34";
  result = 
    strLib.findStr(source,sourceIndex,sourceLength,"34");
  // result = 0, sourceIndex = 3

Related reference
String handling (system words)