A substring expression extracts or assigns substrings by using the [ and ] characters.
Substring assignment allows assigning a substring to a variable.
variable[beg.pos.exp, len.exp] = exp
beg.pos.exp | Indicates the starting position in the expression.
|
len.exp | Number of characters to overlay within the string expression.
|
This assigns the letter "a" to the first character of the string variable. After assigning, string contains "abcd".
string = "abcd" string[1,1] = "a"
This 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 program below 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 this program is:
xxabcdef xxbcdef xxbcdef xx abcdef xx bcdef xx abcdef abcdefxx abcdefxx abcdefxx abcdef xx
The program below 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. If the number specified is past the end of the string value, an empty substring value is selected. If the number specified is negative or zero, the substring is assumed to start at character one. |
len.exp | Must evaluate to a number indicating the number of characters to retrieve. If the number specified exceeds the remaining number of characters in the string, the remaining string is selected. If the number specified is negative or zero, an empty substring is selected. |
This 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
This extracts the first three characters of the phone variable and assigns it to the variable area.code.
area.code = phone[1,3]
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. If the specified number is less than 1, 1 is assumed. If it is greater than the number of fields within the string, the number of fields in the string is increased by adding null fields separated by the delimiter so that string a length equal to the number specified in beg.fld.exp. |
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. |
Results in the string "a,x,y,z".
string = "a,b,c,d" string[",",2,3] = "x,y,z"
Results in the string "a,b,c,x,".
string = "a,b,c,d" string[",",4,2] = "x"
Results in the string "a,x,y,d".
string = "a,b,c,d" string[",",2,2] = "x,y,z"
Results in the string "a,b,x,y,c,d".
string = "a,b,c,d" string[",",3,0] = "x,y"
Results in the string "a,b,x,y,d".
string = "a,b,c,d" string[",",3,-1] = "x,y"
Results in the string "a,b,,x".
string = "a,b" string[",",4,-5] = "x"
Results in the string "a,,d".
string = "a,b,c,d" string[",",2,-2] = ""