Using the Brackets Operator for Insertion
Description
Replaces a substring in variable with a specified string.
Syntax
variable[start, length] = expression
Parameters
The brackets operator has the following parameters when used for insertion. Use start and length to define the substring.
Parameter | Description | |
---|---|---|
Start | A positive or negative integer. * If start is a positive number, the operation will begin at the character in the start position, counting right from the first character or space at the beginning of the string. * If start is a negative number, the operation will begin at the character in that position, counting left from the last character or space at the end of the string. * If start is 0 (zero) or 1, the operation will begin with the first character or space at the beginning of the string. | |
Length | Number of characters to replace, beginning from start. Each space and character within the string is counted as a character position. Also, note that the value specified for length includes the character position specified by start. Note: Length can be negative, signifying reverse order. | |
expression | Replaces the substring in variable that has been defined by the parameters start and length. If expression yields a string that is longer than the specified substring, the total string length of variable will increase. If expression yields a shorter string, then the total string length of variable decreases. In other words, the entire substring defined by start and length is replaced by whatever is the value of expression. |
Example
The following example The following example shows how to use the brackets operator for insertion. * When LENGTH = 0. This yields "ABXXYYCDEF" string = "ABCDEF" string[-4,0] = "XXYY" /* When LENGTH = negative number, the reverse order: yields "A123EF" */ string = "ABCDEF" string[4,-3] = "123"