Manipulating Substrings
The STRPOS, STRPUT, and STRMID routines locate, insert, and extract substrings from their string arguments.
The STRPOS function is used to search for the first occurrence of a substring. It has the form:
result = STRPOS(object, search_string[, pos])
where object is the string to be searched, search_string is the substring to search for, and pos is the character position (starting with position 0) at which the search is begun. The argument pos is optional. If it is omitted, the search is started at the first character (character position 0). The following statements count the number of times that the word dog appears in the string dog cat duck rabbit dog cat dog:
; The string to search—dog appears 3 times.
ANIMALS = 'dog cat duck rabbit dog cat dog'
; Start searching in character position 0
I = 0
; Number of occurrences found
CNT = 0
; Search for an occurrence
WHILE (I NE -1) DO BEGIN
I = STRPOS(ANIMALS, 'dog', I)
; If one is found, count it and advance to the next character
; position.
IF (I NE -1) THEN BEGIN CNT = CNT + 1 & I = I + 1 & END  
ENDWHILE
PRINT, 'Found ', cnt, " occurrences of 'dog'"
Running the above statements produces the result:
Found 3 occurrences of 'dog'
The STRPUT procedure inserts the contents of one string into another. It has the form:
STRPUT, destination, source [, position]
where destination is the string to be inserted into, source is the string to be inserted, and position is the first character position within destination at which source will be inserted. The argument position is an optional argument. If it is omitted, the insertion is started at the first character (character position 0). The following statements use STRPOS and STRPUT to replace every occurrence of the word dog with the word CAT in the string dog cat duck rabbit dog cat dog:
; The string to modify—dog appears 3 times.
ANIMALS = 'dog cat duck rabbit dog cat dog'
WHILE (((I = STRPOS(ANIMALS, 'dog'))) NE -1) DO STRPUT, $
ANIMALS, 'CAT', I
; While any occurrence of dog exists, replace it.
PRINT, ANIMALS
Running the above statements produces the result:
CAT cat duck rabbit CAT cat CAT
The STRMID function extracts substrings from a larger string. It has the form:
result = STRMID(expression, position, length)
where expression is the string from which the substring will be extracted, position is the starting position within expression of the substring (the first position is position 0), and length is the length of the substring to extract. If there are not length characters following position, then the substring will be truncated. The following statements use STRMID to print a table matching the number of each month with its three-letter abbreviation:
; String containing all the month names.
MONTHS = 'JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'
; Extract each name in turn. The equation (I–1)* 3 calculates
; the position within MONTH for each abbreviation.
FOR I = 1, 12 DO PRINT, I,'     ', $
STRMID(MONTHS, (I - 1) * 3, 3)
The result of executing these statements is:
 
1
JAN
2
FEB
3
MAR
4
APR
5
MAY
6
JUN
7
JUL
8
AUG
9
SEP
10
OCT
11
NOV
12
DEC