FIELD()

FIELD(), FIELDS()

Top  Previous  Next

 

The FIELD() function returns one or more delimited substrings from a string. The FIELDS() function is similar to FIELD() but operates on a multivalued string, returning a similarly structured dynamic array of results.

 

 

Format

 

FIELD(string, delimiter, occurrence {, count})

 

where

 

stringis the string from which substrings are to be extracted.

 

delimiterevaluates to the delimiter character.

 

occurrenceevaluates to the position of the substring to be extracted. If less than one, the first substring is extracted.

 

countevaluates to the number of substrings to be extracted. If omitted or less than one, one substring is extracted. Specifying the count as an asterisk extracts all elements of the string from the given start position.

 

 

The FIELD() function extracts count substrings starting at substring occurrence from string. Substrings within string are delimited by the first character of delimiter. If delimiter is a null string, the entire string is returned.

 

If the value of occurrence is greater than the number of delimited substrings in string, a null string is returned.

 

If the value of count is greater than the number of delimited substrings in string starting at substring occurrence, the remainder of string is returned. Additional delimiters are not inserted.

 

 

There is an alternative syntax,

string[delimiter, occurrence, count]

that performs exactly the same operation. In this for, the count is not optional. See QMBasic Expressions and Operators.

 

 

The COL1() and COL2() functions can be used to find the character positions of the extracted substring. When using the FIELDS() function, the values returned by the COL1() and COL2() functions relate to the final extracted item.

 

Use of the $NOCASE.STRINGS compiler directive makes the delimiter case insensitive.

 

 

Examples

 

A = "1*2*3*4*5"

S = FIELD(A, '*', 2, 3)

 

This program fragment assigns the string "2*3*4" to variable S.

 

 

A = "1*2*3*4*5"

S = FIELD(A, '*', 2, *)

 

This program fragment assigns the string "2*3*4*5" to variable S.

 

 

 

See also:

COL1(), COL2(), FIELDSTORE(), LAST()