String Utilities

%TRIM

The %TRIM utility removes leading and trailing whitespace (spaces and tabs) from a string. You can use the %TRIM utility in Direct Mode or include it in a source application program in the following format:

$$FUNC^%TRIM(exp)

You can also use %TRIM as a command line utility to read from STDIN and write to STDOUT in the following format:

%XCMD 'do ^%TRIM'

Utility Labels

The following labels invoke variations of %TRIM as an extrinsic function.

FUNC(s): Returns a string after removing leading and trailing whitespaces from the argument.

L(s): Returns a string after removing leading whitespaces from the argument.

R(s): Returns a string after removing trailing whitespaces from the argument.

Example:

GTM>set strToTrim=$char(9,32)_"string with spaces and tabs"_$char(32,32,32) write $length(strToTrim) 36 GTM>write "strToTrim=",?24,"""",strToTrim,"""",!,"$$L^%TRIM(strToTrim)=",?24,"""",$$L^%TRIM(strToTrim),"""",!,"$$R^%TRIM(strToTrim)=",?24,"""",$$R^%TRIM(strToTrim),"""",!,"$$FUNC^%TRIM(strToTrim)=",?24,"""",$$FUNC^%TRIM(strToTrim),""""
strToTrim= " string with spaces and tabs "
$$L^%TRIM(strToTrim)= "string with spaces and tabs "
$$R^%TRIM(strToTrim)= " string with spaces and tabs"
$$FUNC^%TRIM(strToTrim)="string with spaces and abs"

This example invokes %TRIM as an extrinsic function and demonstrates the use of its L,R, and FUNC labels.

Example:

$ echo " GT.M Rocks! " | gtm -r %XCMD 'do ^%TRIM'

GT.M Rocks!

$

This example invokes %TRIM as a command line utility which reads STDIN and writes the trimmed output to STDOUT.

%MPIECE

The %MPIECE utility replaces one or more consecutive occurrences of the second argument in the first argument with one occurrence of the third argument. This lets $PIECE operate on the resulting string like UNIX awk.

You can use the %MPIECE utility in Direct Mode or include it in a source application program in the following format:

$$^%MPIECE(str,expr1,expr2)

If expr1 and expr2 are not specified, %MPIECE assumes expr1 to be one or more consecutive occurrences of whitespaces and expr2 to be one space.

%MPIECE removes all leading occurrences of expr1 from the result.

Utility Labels

$$SPLIT^%MPIECE(str,expr1): Invokes %MPIECE as an extrinsic function that returns an alias local array of string divided into pieces by expr1. If expr1 is not specified, MPIECE assumes expr1 to be one or more consecutive occurrences of whitespaces.

Example:

GTM>set strToSplit=" please split this string into six"

GTM>set piecestring=$$^%MPIECE(strToSplit," ","|") zwrite strToSplit,piecestring write $length(piecestring,"|")
strToSplit=" please split this string into six"
piecestring="please|split|this|string|into|six
6
GTM>set *fields=$$SPLIT^%MPIECE(strToSplit) zwrite fields
fields(1)="please"
fields(2)="split"
fields(3)="this"
fields(4)="string"
fields(5)="into"
fields(6)="six"