37.6.5 Specifying substrings in expressions

You can determine the number of characters in a macro variable, and use string operators to extract substrings from the value of the variable. Table 37-9 lists several of the string operators and shows how they are used in macro expressions.

See also:

§37.6.2 Understanding operands and operators

Table 37-6 Operators for HTML macro expressions

Table 37-9 String operators in macro expressions

Operator

Macro expression

Result of expression

Type

Value

length
($$string length)

Integer

Number of characters in $$string

char
($$string char N)

String

Nth character in $$string, counting from the left; the leftmost character is number 1

first
($$string first N)

String

First N characters of $$string

last
($$string last N)

String

Last N characters of $$string

before
($$string before $$str)

String

Substring that precedes the first (leftmost) occurrence of $$str in $$string

after
($$string after $$str)

String

Substring that follows the first (leftmost) occurrence of $$str in $$string

starts
($$string starts $$str)

Boolean

True if $$str is at the start of $$string

ends
($$string ends $$str)

Boolean

True if $$str is at the end of $$string

contains
($$string contains $$str)

Boolean

True if $$str occurs anywhere in $$string

trim first
($$string trim first N)

String

All but the first N characters of $$string

trim last
($$string trim last N)

String

All but the last N characters of $$string

replace with
($$string replace " " with 
"_")

String

Each instance of first operand is replaced with second operand

upper
($$string upper)

String

$$string is all uppercase

lower
($$string lower)

String

$$string is all lowercase

For example, to trim off the first four characters of $$mystring:

<$$mystring = ($$yourstring trim first 4)>

If the value of $$yourstring is “makework”, the value of $$mystring would be “work”.

Implied value of second operand

If the second operand N is missing from an expression that uses one of the following operators, a value of 1 (one) is assumed for N:

char
first
last
trim first
trim last

For example, to select only the last character, you can omit the second operand:

<$$yourstring = ($$mystring last)>

If the value of $$mystring is “groceries”, the value of $$yourstring would be “s”.

Previous Topic:  37.6.4.3 Using loop structures

Next Topic:  37.6.6 Using list variables in expressions

Parent Topic:  37.6 Using expressions in macros

Sibling Topics:

37.6.1 Understanding macro expressions

37.6.2 Understanding operands and operators

37.6.3 Displaying expression results in output

37.6.4 Using control structures in expressions

37.6.6 Using list variables in expressions

37.6.7 Using indirection in expressions

37.6.8 Removing spaces from strings: an example