Previous Application Programming: Strings Next

Case Folding

The STRLOWCASE and STRUPCASE functions are used to convert arguments to lowercase or uppercase. Where String is the string to be converted, they have the form:

S = STRLOWCASE(String)

S = STRUPCASE(String)

The following IDL statements generate a table of the contents of TREES showing each name in its actual case, lowercase and uppercase:

; Create array of trees.  
trees = ['Beech', 'Birch', 'Mahogany', 'Maple', 'Oak', $  
         'Pine', 'Walnut']  
  
FOR I=0, 6 DO PRINT, trees[I], STRLOWCASE(trees[I]),$  
STRUPCASE(trees[I]), FORMAT = '(A, T15, A, T30, A)'  

The resulting output from running this statement is as follows:

 

Beech
beech
BEECH
Birch
birch
BIRCH
Mahogany
mahogany
MAHOGANY
Maple
maple
MAPLE
Oak
oak
OAK
Pine
pine
PINE
Walnut
walnut
WALNUT

A common use for case folding occurs when writing IDL procedures that require input from the user. By folding the case of the response, it is possible to handle responses written in uppercase, lowercase, or mixed case. For example, the following IDL statements can be used to ask "yes or no" style questions:

; Create a string variable to hold the response.  
answer = ''  
  
; Ask the question.  
READ, 'Answer yes or no:  ', answer  
IF (STRUPCASE(answer) EQ 'YES') THEN $  
   ; Compare the response to the expected answer.  
   PRINT,'YES' ELSE PRINT, 'NO'  

  IDL Online Help (March 06, 2007)