[ ]= Statement

The substring assignment statement replaces a part of a string.

Format

string [expr1,expr2] = expr3

Parameter(s)

string

String variable to be changed.

expr1

An expression evaluating to the starting character position.

expr2

An expression evaluating to the ending character position.

expr3

An expression evaluating to the replacement string.

Description

The substring assignment statement allows any part of a string to be reassigned to another value. The substring assignment statement may be used with the FIELD, COL( ), and COL2( ) functions to provide the same performance as the REPLACE, INSERT, and DELETE dynamic array functions.

The behavior of the substring assignment is dependent on the values of expr1 and expr2. The rules are as follows:

expr1 >= 0

If expr1 is nonnegative, it is taken as the starting character position of the string from left to right. If expr1 evaluates to 0, the starting character position is 1. For example, if STRING is "HI THERE", then

STRING [2,2 ] = " OW "

produces "HOWTHERE".

If expr1 evaluates to a number greater than the length of the string, then the replacement string is appended at the end.

expr1 < 0

If expr1 is negative, it is taken as the starting character position from right to left. For example, if STRING is "HI THERE", then

STRING [-2,2 ] = " OW "

produces "HI THEOW". If the absolute value of expr1 is greater than the length of the string and expr1 is negative, it behaves as if expr1 were 0.

expr2 >= 0

If expr2 is nonnegative, it is taken as the length of the string to be replaced. Note that this length does not have to correspond with the length of the replacement string. If expr2 evaluates to zero, then the replacement string should be inserted without replacing any characters in the string. (If expr1 is negative, it is inserted to the left of that position; otherwise it is inserted to the right.)

expr2 < 0

If expr2 is negative, it is taken as the ending character position of the string portion to be replaced, counting from right to left. For example, if STRING is "HI THERE", then:

STRING [2, -2 ] = "OW"

produces "HOWE". Similarly, STRING [-2, -2 ] = "OW " produces "HI THERWE".

If the positions specified by expr1 and expr2 overlap, characters are repeated in the resulting string. For example, if STRING is "HI THERE", then:

STRING[7,-7] = "OW"

produces:

"HI THEOW THERE"

With the statement:

DIGITS[n , m] = "XX"

and the variable DIGITS containing "1234567890", the behavior of the substring assignment statement is summarized by this table:

 

n > 0

n = 0

n < 0

m > 0

Starting at position n, replace the next m characters.

DIGITS [3,4]="XX"

results in:

DIGITS= "12XX7890"

Same as n =1: replace the first n characters.

DIGITS [0,4]="XX"

 results in:

DIGITS= "XX567890"

Starting at the nth position from the end of the string, replace the next m characters.

DIGITS [-6,4]="XX"

 results in:

DIGITS= "1234XX90"

m = 0

Insert the replacement string at position n, with no characters deleted.

DIGITS [3,0]="XX"

results in:

DIGITS = "12XX34567 890"

 

Same as n =1: insert the replacement string at the beginning of the original string, with no characters deleted.

DIGITS [0,0]="XX"

results in:

DIGITS = "XX1234567 890"

Starting at the nth position from the end of the string, insert the replacement string with no characters deleted.

DIGITS [-6,0]="XX"

 results in:

DIGITS= "12345XX67890"

m < 0

Replace all characters from the nth position from the beginning of the string up to the nth position from the end of the string.

DIGITS [3,-4]="XX"

results in:

DIGITS= "12XX890"

Same as n =1: replace all characters from the first position up to the mth position from the end of the string.

DIGITS [0,-4]="XX"

results in:

DIGITS= "XX890"

Starting at the nth position from the end of the string, replace all characters up to the mth position from the end of the string.

DIGITS [-6,-4]="XX"

 results in:

DIGITS= "1234XX890"

Example

In this application, a full name in the string variable NAME is reduced to the first initial and last name. (EQUATE statements are used to differentiate a blank space from the null string for readability.)

EQUATE BLANK TO " ", NIL TO ""

NO. OF WORDS = DCOUNT (NAME, BLANK )

SURNAME = FIELD (NAME, BLANK, NO. OF WORDS)

POSITION = COL1( ) -1

NAME [2, POSITION] = BLANK

See Also

Statement and Function Reference