Well, that's no ordinary rabbit!

String maipulation, extraction, and other mangling

Note: In counting characters remember that the first position is 0 not 1.

Unless otherwise noted assume the variable STR contains the string "THIS IS MAGIC".

Using % (to the right)

%x Returns characters to the right of the x-th character

STR%2 would return "S IS MAGIC"

'%x Returns characters not to the right of the x-th character

STR'%2 would return "THI"

Using $ (to the left)

$x Returns characters to the left of the x-th character

STR$2 would return "TH"

'$x Returns characters not to the left of the x-th character

STR'$2 would return "IS IS MAGIC"

Using # (exact location)

#x Returns the x-th character

STR#8 would return "M"

'#x Returns all characters except for the x-th character

STR'#8 would return "THIS IS AGIC"

This is useful for things like removing the hyphens from a SS#. For this example assume STR contains 123-45-6789

STR'#6'#3 would return 123456789

#"xc" Returns the characters to the left of the x-th occurrence and to the right of the (x-1) occurrence (again start counting with 0) of the character c. Think of this as breaking the string into pieces, the breaking point being the character c. Only punctuation characters may be used.

STR#"0 " would return piece 0 - "THIS"
STR#"1 " would return piece 1 - "IS"
STR#"2 " would return piece 2 - "MAGIC"

One of the most common uses is to break a name field into last and first names. For these examples assume that STR contains "JONES,SARAH J".

STR#"0," would return the last name - "JONES"
STR#"1," would return the first name and middle name/initial - "SARAH J"
STR#"1,"#"0 " would return just the first name - "SARAH"
STR#"1,"#"1 " would return the middle name/initial - "J"

'#"xc" Returns the characters excluding those in the x-th c piece

STR'#"0 " would return all but piece 0 - " IS MAGIC"
STR'#"1 " would return all but piece 1 - "THIS MAGIC"
STR'#"2 " would return all but piece 2 - "THIS IS "

Using L (length or locate)

L returns the length of a string or the location in a string where a specific substring is found. It can be used to determine how long a string is or if it contains another string. L(STR) simply returns the length of the string. L(STR,STR2) returns the location in STR where STR2 starts or the length of STR if it does not contain STR2.

In these examples:
STR = "THIS IS A STRING OF TEXT".
STR2 = "OF"
STR3 = "TOM"

L(STR) would return 24 (if I counted right)
L(STR,STR2) would return 18
L(STR,STR3) would return 24
L(STR,"S") would return 4

One example of it's use would be to check if the patient name has the letter X in it. Why you would want to do that I don't know.

L(@name,"X")<L(@name)

Other Magic Pages



Don't see what you need?  Visit one of these other sites


or email me your request (tomt at thomast357.com).