findStr()

The vgLib.findStr() system function searches for the first occurrence of a substring in a string.

vgLib.findStr() is one of a number of functions maintained for compatibility with earlier versions. New code can use standard EGL operators for these purposes.

Syntax

  vgLib.findStr(
    source CHAR | DBCHAR | MBCHAR | UNICODE | NUM | HEX in,
    sourceSubstringIndex INT inOut,
    sourceSubstringLength INT in,
    searchString CHAR | DBCHAR | MBCHAR | UNICODE | NUM | HEX in)
  returns (result INT)
source
String from which a source substring is derived. Can be a variable or a literal.
sourceSubstringIndex
Identifies the starting byte for the substring in source at which the search starts, given that the index value of the first byte is 1. This index must be an integer variable (INT or BIN(9)). If the searchString is found within the source substring, sourceSubstringIndex is updated to indicate the searchString location.
sourceStringLength
Identifies the number of bytes in the source field. This length can be an integer literal or variable (INT or BIN(9)).
searchString
String variable or literal to be searched for in the source string. Trailing blanks or nulls are truncated from searchString before searching begins as long as there is at least one non-blank or one non-null character. Otherwise the searchString is not truncated.
result
One of the following integer values:
-1
Search string was not found
0
Search string was found

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.

Error considerations

If you use V6 exception compatibility (see Using V6 exception compatibility), the following values are returned in sysVar.errorCode:
8
Index less than 1 or greater than string length.
12
Length less than 1.
20
Double-byte index not valid. Index for a DBCHAR or UNICODE string points to middle of double-byte character.
24
Double-byte length not valid. Length in bytes for a DBCHAR or UNICODE string is odd (double-byte lengths must always be even).

Example

The following example searches for the characters "34" in the source string:

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