Tip: See also the IDL_String::CharAt, IDL_String::Remove, and IDL_String::Substring methods, which provide similar functionality but with an object-oriented interface.

The STRMID function extracts one or more substrings from a string expression. Each extracted string is the result of removing characters from the input string.


Extract a substring, in this case: “is”, from a longer string and store it in another variable. Remember that the first character in the original string is at position zero (0):

myString = "IDL is fun"
subString = STRMID(myString, 4, 2)
PRINT, subString

IDL prints:


Extract a substring from the end of a string (quote from William Shakespeare). The string we want to remove is highlighted in yellow:

myQuote = "It is not in the stars to hold our destiny but in ourselves."
extractedStr = STRMID(myQuote, 29, 12, /REVERSE_OFFSET)
PRINT, extractedStr

IDL prints:

our destiny


Result = STRMID(Expression, First_Character [, Length] , /REVERSE_OFFSET )

Return Value

The result of the function is a string of Length characters taken from Expression, starting at character position First_Character.

The form of First_Character and Length control how they are applied to Expression. Either argument can be a scalar or an array:

  • If a scalar value is supplied for First_Character and Length, then those values are applied to all elements of Expression. The result has the same structure and number of elements as Expression.
  • If First_Character or Length is an array, the size of their first dimension determines how many substrings are extracted from each element of Expression. We call this the “stride”.
  • If both are arrays, they must have the same stride.
  • If First_Character or Length do not contain enough elements to process Expression, STRMID automatically loops back to the beginning as necessary. Excess values are ignored.
  • If the stride is 1, the result will have the same structure and number of elements as Expression. If it is greater than 1, the result will have an additional dimension, with the new first dimension having the same size as the stride.



The expression from which the substrings are to be extracted. If this argument is not a string, it is converted using IDL's default formatting rules.


The starting position within Expression at which the substring starts. The first character position is 0.


The length of the substring. If there are not enough characters left in the main string to obtain Length characters, the substring is truncated. If Length is not supplied, STRMID extracts all characters from the specified start position to the end of the string.



Specifies that First_Character should be counted from the end of the string rather than the beginning. Length then proceeds from this position to the right, toward the end of the string. This allows simple extraction of strings from the end.

Version History



See Also

Comparing Strings, String Operations, String Processing, STRCMP, STREGEX, STRJOIN, STRMATCH, STRPOS, STRPUT, STRSPLIT, STRTRIM, Substrings, IDL_String