Search code examples
stringconcatenationidl-programming-language

Erroneous spaces when concatenating string and integer in IDL


Inputting the following four lines into the IDL console gives the output below.

IDL> num=123
IDL> str="bananas"
IDL> join=str+string(num)
IDL> print,join
bananas     123

Why are 5 spaces appearing in the string and how can I stop it happening?


Solution

  • Numerical values are padded with leading blank spaces when converted with the STRING function. Use STRTRIM instead. The argument "2" removes both leading and trailing whitespace.

    IDL> num=123
    IDL> str="bananas"
    IDL> join=str+STRTRIM(num,2)
    IDL> print,join
    bananas123
    

    The STRTRIM documentation explains more about the extra spaces. This page on formatted output also has details on whitespace padding.