Working With Strings


This tutorial discusses how to manipulate strings in IDL. You will find that IDL has much of the same powerful functionality as Perl when it comes to manipulating strings, especially when combined with IDL's ability to operate on arrays.

A string variable is anything enclosed by single or double quotes. For example,

str1 = 'This is a string.'
str2 = "10.03456"

Both str1 and str2 are strings. You can convert any variable (but not structures) into a string by using the string() function in IDL. Combined with the format keyword you can write whatever you would like to the terminal:

x = 10.0
print, string(x)
print, string(x,format='(I0)')
print, string(x,format='(F5.2)')

You can determine the character length of a string by using the strlen() function:

len = strlen(str1)
print, len

To convert a string to all uppercase or lowercase type:

print, strupcase(str1)
print, strlowcase(str1)

To split one single long string into sub-strings by matching on a pattern type:

substrings = strsplit(str1,' ',/extract)
help, substrings & print, substrings

Setting the extract keyword equal to one tells strsplit() to return the individual sub-strings rather than the array indices (positions) of the individual sub-strings. You can search on much more complicated patterns using the regex keyword in strsplit() (please read the documentation for this function and also check out stregex() for more details). Next, define the string

str3 = '   str ing3 . '
help, str3

To get rid of all the whitespace (and tabs) type

str3crunch = strcompress(str3,/remove)
help, str3crunch

The strtrim() function removes leading and/or trailing blanks from the input string:

help, strtrim(str3)
help, strtrim(str3,0)
help, strtrim(str3,1)
help, strtrim(str3,2)

The first two lines are identical and remove trailing blanks, the third line removes leading blanks, and the fourth line removes both. To find the occurance (index array) of a sub-string use the strpos() function:

str4 = 'redwhiteblue'
print, strpos(str4,'white')

This function returns -1 if the sub-string is not found, so you should have some error-catching in your code to account for this possibility:

pos = strpos(str4,'green')
if pos[0] eq -1L then print, 'Warning: sub-string not found!'

To crop a string at a particular position we can use strmid() in conjunction with strpos(). Let's cut the word "blue" from str4:

str4cut = strmid(str4,0,strpos(str4,'blue'))
print, str4cut

Note how you can conveniently have nested functions. To join (concatenate) strings you can use the + sign or the strjoin() function:

str5 = 'one' & str6 = 'two'
print, str5+str6
print, strjoin([str5,str6])

The function strjoin() has much more functionality than simply concatenating strings, however:

print,'strjoin([str5,str6],' put me in the middle! ')

Alternatively you can use strput to "put" one string into another by specifying the (zero-indexed) position where you want the new sub-string to be placed (the documentation explains this better):

str7 = 'IDL is keen.'
strput, str7, 'neat', 7
print, str7

To compare two strings with one another the strcmp() function comes to the rescue. This function returns 1 (true) if the strings match and 0 (false) if they don't. To ignore the case (upper or lower) of the two strings we use the fold_case keyword and compare the first two letters of the two input strings:

str8 = 'Cat in the hat.'
str9 = 'ca'
print, strcmp(str8,str9,2,/fold_case)

One of my favorite string-matching functions, especially combined with the where() function is strmatch(). Like strcmp(), strmatch() also returns true or false if a match is found. The power of strmatch() comes from being able to use wildcards:

str10 = ['bob','billy','wiley','sesame','bobo']
print, strmatch(str10,'b*')
match = where(strmatch(str10,'b*') eq 1,nmatch)
if nmatch ne 0L then print, match, str10[match]

The Goddard library contains a number of other useful string manipulation routines. Many of these were developed before IDL added much of the string functionality available in 6.0, but these routines are still very useful. To replace all occurances of a certain sub-string in a string with a new sub-string type:

fitslist = 'image'+['01','02','03','04']+'.fits'
print, fitslist
pslist = repstr(fitslist,'.fits','.ps')
print, pslist

Finally, to remove a specify character from a string type

str11 = '1:2:3:4:5'
remchar, str11, ':'
print, str11




Last update 2003 November. Email J. Moustakas with questions or comments.