A substring expression extracts or assigns substrings by using the [ and ] characters.
Substring assignment allows assigning a substring to a variable.
Example 1
variable[beg.pos.exp, len.exp] = exp
Example 2
variable[len.exp] = exp
beg.pos.exp |
Indicates the starting position in the expression.
|
len.exp | Number of characters to overlay within the string
expression. Syntax Example 1
Syntax Example 2 When the beg.pos.exp position is omitted, then the right-most length expression characters are replaced in the string. |
This example assigns the letter "a" to the first character of the string variable. After assigning the letter "a", the string contains "abcd".
string = "abcd" string[1,1] = "a"
This example results in the string, 'axxxd'. Notice that the additional characters beyond the length of the overlay are inserted.
string = 'abcd' string[2,2] = 'xxx'
The following program example tests various combinations of substring replace.
string = "abcdef" s = string ; s[ 0 , 0]= "xx"; crt s s = string ; s[ 0 , 1]= "xx"; crt s s = string ; s[ 1 , 1]= "xx"; crt s s = string ; s[-1 , 0]= "xx"; crt s s = string ; s[-1 , 1]= "xx"; crt s s = string ; s[-1 ,-1]= "xx"; crt s s = string ; s[ 7 , 0]= "xx"; crt s s = string ; s[ 7 , 1]= "xx"; crt s s = string ; s[ 7 ,-1]= "xx"; crt s s = string ; s[ 8 , 1]= "xx"; crt s
The output of the previous program example is:
xxabcdef xxbcdef xxbcdef xx abcdef xx bcdef xx abcdef abcdefxx abcdefxx abcdefxx abcdef xx
The following program example outputs as:
string="#####"; s=string for x=3 to 7 for j=3 to 7 s[x,j]='..' crt s next j next x end
Substring extraction allows the extraction of a string from within another string.
str.exp[beg.pos.exp, len.exp]
beg.pos.exp | Must evaluate to a number indicating the starting position within the
string.
|
len.exp | Must evaluate to a number indicating the number of characters to
retrieve.
|
Example 1
This example tests the first character of response to determine if it is the letter y. If it is, the printer on statement is issued.
if response[1,1] = "y" then printer on
Example 2
This example extracts the first three characters of the phone variable and assigns it to the variable area.code.
area.code = phone[1,3]
Example 3
This example extracts the last four characters of the phone variable.
suffix = phone[4]
Substring field store allows changing one or more fields or substrings within a character string variable that are delimited by a specific character.
var[delimiter, beg.fld.exp, num.flds.exp] = substr
var | String expression or variable. |
delimiter | Delimiter character separating fields or substrings in the original string. |
beg.fld.exp | Starting field within the string for the substring to be placed.
|
num.flds.exp |
Number of fields or substrings to be replaced in string.
|
substr | Substring or group of substrings (separated by a delimiter) to be substituted. Multiple substrings separated by the specified delimiter can be substituted in the original string. |
Example 1
The following example results in the string "a,x,y,z".
string = "a,b,c,d" string[",",2,3] = "x,y,z"
Example 2
The following example results in the string "a,b,c,x,".
string = "a,b,c,d" string[",",4,2] = "x"
Example 3
The following example results in the string "a,x,y,d".
string = "a,b,c,d" string[",",2,2] = "x,y,z"
Example 4
The following example results in the string "a,b,x,y,c,d".
string = "a,b,c,d" string[",",3,0] = "x,y"
Example 5
The following example results in the string "a,b,x,y,d".
string = "a,b,c,d" string[",",3,-1] = "x,y"
Example 6
The following example results in the string "a,b,,x".
string = "a,b" string[",",4,-5] = "x"
Example 7
The following example results in the string "a,,d".
string = "a,b,c,d" string[",",2,-2] = ""