IB Statements/sub

From CometWiki

Jump to: navigation, search

'SUB function'Bold text

Syntax: SUB(string-argument, starting-position, length)

Discussion: The SUB function returns a substring of a specified string-argument.

The string-argument is the string value from which the substring is to be derived. It may be a string constant, a single-element string variable, a string array element, a string expression, or a string function.

The starting-position is a numeric value (constant, variable, expression, or function) that indicates the starting character position of the substring within the string argument. The starting-position may be a numeric constant, a single-element numeric variable, a numeric array element, a numeric expression, or a numeric function.

Length is a numeric value (constant, variable, expression, or function) that indicates the length of the substring. The length argument may be a numeric constant, a single-element numeric variable, a numeric array element, a numeric expression, or a numeric function. Note: If the length argument is zero or negative, the SUB function will return a null value.

See RSUB.

Example 1: A$ = SUB(B$,5,2)

The above example returns a substring from the string variable B$. The substring will contain two characters of data from B$ staring at character position 5. This value will be moved to the string variable A$.

For example, if B$ equals "SIGNATURE SYSTEMS" A$ will equal "AT".

Example 2: A$ = SUB("GoodBad Ugly",(4*X-3),4)

This example shows how an expression can be used to determine the starting-position in the string-argument. In this case, the string-argument is a 12-character field equal to:

"GoodBad Ugly".

The starting-position will be determined by the value of the expression (4*X-3). For example, if the numeric variable X equals 1, the expression (and hence the starting-position) will equal 1.

If X equals 2, the starting-position will equal 5 (the value of the expression). And, if X equals 3, the starting-position will equal 9.

So, depending on the value of X, different starting-positions may be specified automatically. The length argument in the above example is a constant 4 characters.

Thus, if X equals 1, the 4-character substring assigned to A$ will equal "Good" (because the starting-position will be 1 and the length is 4).

If X equals 2, A$ will equal "Bad " (note the trailing blank), and if X equals 3, A$ will equal "Ugly".

Application note: This programming technique may be a very useful means to create conditional messages based on the value of a numeric data variable.

Personal tools