trim()


Remove unwanted characters from text.

Syntax

trim(str,chars,[behavior])

Parameter

Parameter

Definition

str

string to be returned without specified characters The str can be a text string enclosed in double quotation marks, or one or more PSL commands that produce text as output.

chars

one or more characters that are to be removed from the copy of str output by the trim() function

behavior

optional argument that defines the behavior of the PSL trim() function The value of behavior is specified as either an integer or one of the following constants:

  • 0 TRIM_ALL - trims all occurrences of chars from str
  • 1 TRIM_LEADING - trims chars from the beginning of the string, stopping when it encounters a character that is not specified in chars
  • 2 TRIM_TRAILING - only trims chars from the end of the string
  • 3 TRIM_LEADING_AND_TRAILING - trims chars from the start and the end of the string
  • 4 TRIM_REDUNDANT - trims redundant chars from the string when they are adjacent to each other When redundant characters are removed, one of the characters is left in the string. Thus, if you had three characters in a row, you would have one after the trim() function removed redundant characters.

Default
0 (TRIM_ALL)|

 

Description

The trim() function returns a copy of str with specified characters removed. The trim() function allows you to remove specified characters from the entire string, the beginning of the string, the end of the string, or both the beginning and end of the string. The trim() function will also remove redundant characters in the string.

Example

The following example demonstrates the trim() function by manipulating a string using all behaviors of the trim() function:

$include "stdpsl.ph"
using stdpsl;
void_t main() {
string_t string, trimmed_string;
string= "LEADING\nXX\nTh$is \nlo^oks \nbet!ter \nwith \nunw*anted \n" .
"char##acters \nremo@ved..\nXX\nTRAILING";
trimmed_string = trim(string,"\n$^!*#@");
print(string,"\n");
print("\n",trimmed_string);
trimmed_string = trim(trimmed_string,"LEADING",TRIM_LEADING);
print("\n",trimmed_string);
trimmed_string = trim(trimmed_string,"TRAILING",TRIM_TRAILING);
print("\n",trimmed_string);
trimmed_string = trim(trimmed_string,"XX",TRIM_LEADING_AND_TRAILING);
print("\n",trimmed_string);
trimmed_string = trim(trimmed_string,".",TRIM_REDUNDANT);
print("\n",trimmed_string);
}

This example produces the following output:

LEADING
XX
Th$is
lo^oks
bet!ter
with
unw*anted
char##acters
remo@ved..
XX
TRAILING
LEADINGXXThis looks better with unwanted characters removed..XXTRAILING
XXThis looks better with unwanted characters removed..XXTRAILING
XXThis looks better with unwanted characters removed..XX
This looks better with unwanted characters removed..
This looks better with unwanted characters removed.