strtrim(X;Y;C;D)

Returns the given string with certain characters trimmed from either or both ends.

Syntax

strtrim(X;Y;C;D)
bstrtrim(X;Y;C;D)

Input

Argument Type Description
X text The string on which to apply the function

A scalar value or the name of a column

Y text Y is the string containing a set of characters to trim (or not trim) from X

A scalar value or the name of a column

C integer Specifies whether the function trims X of all characters that are either found in Y or not found in Y

If C=0, all characters that are found in Y are trimmed.

If C=1, all characters that are not found in Y are trimmed.

D integer Specifies in which direction the trimming is performed

If D=1, trimming proceeds from the left.

If D=-1, trimming proceeds from the right.

If D=0, trimming proceeds from both directions.

Return Value

Returns the text value corresponding to the string X with characters from the string Y trimmed from the left, the right, or both ends.

The function searches X for any character that is present in Y (if C=0) or not present in Y (if C=1).

The trimming begins from the left (i.e., the first matching characters in X are trimmed) if D=1. The trimming begins from the right (i.e., the last matching characters in X are trimmed), if D=-1. Trimming proceeds from both ends if D=0.

If X is N/A, then the result is N/A.

If Y is N/A:
  • If C=0, the result is X unchanged.
  • If C=1, the result is N/A.

Sample Usage

value string2 present direction strtrim(value;string2;present;direction)
'apple' 'aeiou' 0 1 'pple'
'apple' 'aeiou' 0 -1 'appl'
'apple' 'aeiou' 0 0 'ppl'
'apple' 'aeiou' 1 0 'apple'
'' 'aeiou' 0 1 ''
'apple' '' 0 -1 'apple'

Example

This example demonstrates how strtrim(X;Y;C;D) can be used to trim trailing spaces from the end of a string.

<base table="default.lonely"/>
<willbe name="test" value="'a b c d e f g     '"/>
<willbe name="test_cnt" value="strcount(test;' ')"/>
<willbe name="test_trim" value="strtrim(test;' ';0;-1)"/>
<willbe name="test_trim_cnt" value="strcount(test_trim;' ')"/>

The value of the string in the test column has five trailing spaces. The value in the test_cnt column is 11, which is the total number of spaces in the string in the test column.

The call to strtrim(test;' ';0;-1) in the test_trim column takes the value in the test column and trims all spaces starting from the rightmost character. The value in the test_trim_cnt column is 6, which verifies that the trailing spaces no longer exist in the string in the test_trim column.

Additional Information

  • strtrim is Unicode (UTF-8) compliant and will work with Unicode or plain ASCII text fields.
  • If passed a string argument that is not legal Unicode, it will by default signal an error (configurable as a user preference).
  • A corresponding function bstrtrim can be used with non-Unicode strings (e.g., binary or legacy encodings).