TRIM$ / LTRIM$ / RTRIM$

Syntax

Syntax: TRIM$(string$)   LTRIM$(string$)   RTRIM$(string$)
Source: pb_cg_emit_trim_builtin (pb_codegen.c:3066) + pb_cg_emit_trim_temp (pb_codegen.c:1930)

Description

Removes whitespace characters from a string. Three variants are available:

Whitespace characters recognized are: space (ASCII 32), horizontal tab (ASCII 9), line feed (ASCII 10), and carriage return (ASCII 13). Non-whitespace characters are preserved, including their case and any internal whitespace. The original string is not modified.

These functions are essential for cleaning user input, parsing delimited data where fields may have surrounding whitespace, normalizing file lines read with trailing CR/LF, and preparing strings for comparison or storage.

Parameters

ParameterDescription
string$The STRING expression to trim. Can be a literal, variable, or expression. The original string is unchanged.

Return Value

A new STRING with whitespace removed according to the function variant.

Example

FUNCTION PBMAIN() AS LONG
    LOCAL raw$, result$ AS STRING
    
    ' Strings with surrounding whitespace
    raw$ = "   Hello World   "
    
    result$ = TRIM$(raw$)       ' result$ = "Hello World"
    PRINT "TRIM$:  ["; result$; "]"
    
    result$ = LTRIM$(raw$)      ' result$ = "Hello World   "
    PRINT "LTRIM$: ["; result$; "]"
    
    result$ = RTRIM$(raw$)      ' result$ = "   Hello World"
    PRINT "RTRIM$: ["; result$; "]"
    
    ' Mixed whitespace: tabs, spaces, newlines
    raw$ = CHR$(9) + CHR$(9) + "  Data  " + CHR$(13) + CHR$(10)
    result$ = TRIM$(raw$)       ' result$ = "Data"
    PRINT "Mixed WS trimmed: ["; result$; "]"
    
    ' All whitespace becomes empty string
    raw$ = "       "
    result$ = TRIM$(raw$)       ' result$ = ""
    PRINT "All WS: ["; result$; "]"
    
    ' Practical: clean user input
    raw$ = "    John Smith    "
    result$ = TRIM$(raw$)       ' result$ = "John Smith"
    PRINT "Cleaned name: ["; result$; "]"
    
    ' Internal whitespace is preserved
    raw$ = "  Hello   World  "
    result$ = TRIM$(raw$)       ' result$ = "Hello   World"
    PRINT "Internal WS kept: ["; result$; "]"
    
    FUNCTION = 0
END FUNCTION

Generated Assembly

Source: pb_cg_emit_trim_builtin (pb_codegen.c:3066) + pb_cg_emit_trim_temp (pb_codegen.c:1930)
; Copy source to scratch buffer
lea    rcx, [rbp - asciiz_scratch]
mov    rdx, [rbp - print_ptr_offset]
mov    r8, capacity
call   [__imp_lstrcpynA]

; Find first non-whitespace (leading)
xor    rax, rax                        ; index = 0
__pb_trim_lead_loop:
cmp    rax, [rbp - len_offset]
jae    __pb_trim_all_ws_N              ; all whitespace -> empty
mov    r9b, byte [r11 + rax]           ; buffer[index]
cmp    r9b, 32                         ; space
je     __pb_trim_lead_next
cmp    r9b, 9                          ; tab
je     __pb_trim_lead_next
cmp    r9b, 10                         ; LF
je     __pb_trim_lead_next
cmp    r9b, 13                         ; CR
je     __pb_trim_lead_next
jmp    __pb_trim_lead_done
__pb_trim_lead_next:
inc    rax
jmp    __pb_trim_lead_loop
__pb_trim_lead_done:

; Find last non-whitespace (trailing)
mov    rdx, [rbp - len_offset]
__pb_trim_trail_loop:
cmp    rdx, rax
jbe    __pb_trim_bounds_done           ; crossed over
; Check buffer[rdx-1] for whitespace, dec if so
; ...
; Shift left if leading whitespace removed
; NUL-terminate at new position

Notes