String Utilities

%TRIM

%TRIM removes leading and trailing characters from a string. The format of the %TRIM utility function is:

$$FUNC|$$L|$$R^%TRIM(expr1[,expr2])
  • The first expression specifies the string. The optional second expression specifies a list of trailing and leading characters to remove from expr1. When expr2 is not specified, ^%TRIM assumes expr2 as $char(9,32) which removes all trailing and leading whitespaces (spaces and tabs) from expr1. Note that ^%TRIM treats expr2 as a list of characters (not a substring).

  • The $$FUNC label trims leading and trailing characters.

  • The $$L label trims leading characters.

  • The $$R label trims trailing characters.

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

echo "  string with leading and trailing spaces  " | $gtm_dist/mumps -r ^%TRIM

Example:

GTM>set strToTrim=$char(9,32)_"string with spaces and tabs"_$char(32,32,32) write $length(strToTrim) 
32 
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 tabs"

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_dist/mumps -r ^%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"