# Selecting Characters from Variables

The format for selecting characters from a variable can be expressed as:

-       ${variable_name[:offset[:length]]}

If you want to select the first N characters from the string assigned to a variable, simply append a colon and the number of characters to skip from the beginning of the string to the variable name.

-       ; Remove the first character of extension, save in "number" variable
        exten => _9X.,1,Set(number=${EXTEN:1})

Assuming we've dialed 918005551234, the value saved to the 'number' variable would be 18005551234. This is useful in situations when we require users to dial a number to access an outside line, but do not wish to pass the first digit.

If you use a negative offset number, Asterisk starts counting from the end of the string and then selects everything after the new position. The following example will save the numbers 1234 to the 'number' variable, still assuming we've dialed 918005551234.

-       ; Remove everything before the last four digits of the dialed string
        exten => _9X.,1,Set(number=${EXTEN:-4})

We can also limit the number of characters from our offset position that we wish to use. This is done by appending a second colon and length value to the variable name. The following example will save the numbers 555 to the 'number' variable.

-       ; Only save the middle numbers 555 from the string 918005551234
        exten => _9X.,1,Set(number=${EXTEN:5:3})

The length value can also be used in conjunction with a negative offset. This may be useful if the length of the string is unknown, but the trailing digits are. The following example will save the numbers 555 to the 'number' variable, even if the string starts with more characters than expected (unlike the previous example).

-       ; Save the numbers 555 to the 'number' variable
        exten => _9X.,1,Set(number=${EXTEN:-7:3})

If a negative length value is entered, Asterisk will remove that many characters from the end of the string.

-       ; Set pin to everything but the trailing #.
        exten => _XXXX#,1,Set(pin=${EXTEN:0:-1})