A substring is a set of characters that makes up part of a whole string. The syntax to specify a substring is:
substring= string[m, n]
where string is a string variable, m is the starting character position, and n is the substring length. For example, if the current value of variable S is the string "ABCDEFG", then the current value of S[3,2] is the substring "CD" (the two-character substring starting at character position 3 of string S).
If the starting character specification is past the end of the string value, then an empty substring value is selected. If the starting character specification is negative or zero, then the substring is assumed to start at character one.
If the substring length specification exceeds the remaining number of characters in the string, then the remaining string is selected. If the substring length specification is negative or zero, then an empty substring is selected.
A segment of a character string can be changed or substituted without affecting the remainder of the string by assigning the new segment to the variable containing the original string. The new segment is assigned to the original string variable with the starting character and segment length specified with the variable. The string variable can be a single variable or an array element. For example, if the current value of the variable S above ("ABCDEFG") is to have characters 3 to 5 inclusive replaced by the string "123", the assignment S[3,3]="123" accomplishes this substitution so that the variable S now becomes "AB123FG". The general syntax for substring replacement is:
string[m, n] = substring
where string is a string variable, substring is the substring to be inserted, m is the starting character position, and n is the number of characters in the string that are replaced.
One or more fields or substrings within a character string variable that are delimited by a specific character can be changed by a single assignment.
string[delimiter, m, n] = substrings
where string is a string expression or variable, substrings is one or more substrings (separated by a delimiter) to be substituted, delimiter is the delimiter separating fields or substrings in the original string, m is the starting field position within the string for the substring to be placed, and n is the number of fields or substrings to be replaced.
If m is less than 1, 1 is assumed. If m is greater than the number of fields within string then the number of fields in string is increased by adding null fields separated by the delimiter so that string has the length of m fields. Multiple substrings separated by the specified delimiter can be substituted in the original string.
The expression n has different effects depending on whether it is positive, negative or zero. If it is positive, then n fields in string are replaced with n substrings. If m + n is greater than the number of fields in string, then the number of fields in the resulting string is increased to the value m + n, and the extra substrings are concatenated to the end of the original string. If m + n is less than the number of fields in string, but n is greater than the number of substrings specified, only the specified substrings are substituted and the remainder of the n fields in string are nulled.
If n is zero, then no fields in string are deleted and the specified substrings are inserted into string before the mth field. If n is less than zero, then n fields greater than or equal to m are deleted from string and the entire expression containing the substrings is inserted at that point in the string.