Monday, February 9, 2009

Asterisk Substrings

Substrings 
  ${foo:offset:length} 

returns a substring of the string foo, beginning at offset offset and returning the next length characters. The first character is at offset 0. 
If offset is negative, it is taken leftwards from the right hand end of the string. 
If length is omitted or is negative, then all the rest of the string beginning at offset is returned. 

Examples: 

  ${123456789:1} - returns the string 23456789 
  ${123456789:-4} - returns the string 6789 
  ${123456789:0:3} - returns the string 123 
  ${123456789:2:3} - returns the string 345 
  ${123456789:-4:3} - returns the string 678 


Examples of use: 

  exten => _NXX.,1,SetVar(areacode=${EXTEN:0:3}) - get the first 3 digits of ${EXTEN} 
  exten => _516XXXXXXX,1,Dial(${EXTEN:3}) - get all but the first 3 digits of ${EXTEN} 
  exten => 100,1,SetVar(whichVowel=4) 

  exten => 100,2,SetVar(foo=AEIOU:${whichVowel}:1) - sets ${foo} to the single letter 'U'

No comments: