Substring Expressions

A substring expression extracts or assigns substrings by using the [ and ] characters.

Substring Assignment

Substring assignment allows assigning a substring to a variable.

Syntax

variable[beg.pos.exp, len.exp] = exp

Parameter(s)

beg.pos.exp

Indicates the starting position in the expression.

  • If 0 is specified, the expression is inserted at the beginning of the target string.

  • If the number specified is greater than the length of the string, spaces are appended to the end of the string to account for the value of beg.pos.exp prior to the expression being added to the result.

  • If the number specified is less than 0, spaces are inserted between the expression and the string for a length equal to the absolute value of beg.pos.exp.

len.exp

Number of characters to overlay within the string expression.

  • If 0 is specified, the expression is inserted prior to the starting character position within the string.

  • If the number specified is greater than 0, the corresponding number of characters in the string are overlaid.

  • If the number specified is less than 0, then len.exp defaults to 0.

  • If the number specified is greater than the number of characters in the string, starting from the starting position count, then len.exp defaults to the number of characters within the string, minus the starting position count.

  • If the length of the substr.exp to overlay is greater than the len.exp, the additional characters are included in the resulting expression. Only the number of characters specified in len.exp are overlaid. The additional characters are inserted prior to the next position in the original expression.

Example(s)

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

Substring extraction allows the extraction of a string from within another string.

Syntax

str.exp[beg.pos.exp, len.exp]

Parameter(s)

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.

Example(s)

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

Substring field store allows changing one or more fields or substrings within a character string variable that are delimited by a specific character.

Syntax

var[delimiter,beg.fld.exp, num.flds.exp] = substr 

Parameter(s)

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.

  • If the specified number is positive, then that number of fields in the string are replaced with the same number of substrings.

  • If the specified number is 0, no fields in the string are deleted and the specified substrings are inserted into the string before the specified beginning field.

  • If the specified number is less than 0, num.flds.exp fields greater than or equal to beg.fld.exp are deleted from the string and the entire expression containing the substrings is inserted at that point in the string.

  • If beg.fld.exp + num.flds.exp is greater than the number of fields in string, the number of fields in the resulting string is increased to the value beg.fld.exp + num.flds.exp, and the extra substrings are concatenated to the end of the original string.

  • If beg.fld.exp + num.flds.exp is less than the number of fields in string but num.flds.exp is greater than the number of substrings specified, only the specified substrings are substituted and the remainder of the num.flds.exp fields in string are nulled.

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(s)

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] = ""

See Also

Assignment, casing Statement, field() Function, Substring Assignment, Substring Extraction, Substring Field Store, [] Reserved Characters