RIGHT$(string$, n)pb_cg_emit_right_builtin (pb_codegen.c:2863)Returns the rightmost n characters of string$. The function extracts the last n characters from the source string and returns them as a new string. The original string is not modified.
If n is less than 1, an empty string is returned. If n exceeds the length of string$, the entire string is returned. The implementation calls lstrlenA to determine the source length, then computes the starting offset as source_len - n and copies from there.
Common uses include extracting filename extensions, parsing trailing numeric suffixes, getting the last N characters of an identifier, and reading right-justified data from fixed-width formats.
| Parameter | Description |
|---|---|
string$ | The source STRING expression. Can be a literal, variable, or any expression that evaluates to a string. |
n | Number of characters to extract from the right side. A numeric expression (byte, integer, long). Clamped to valid range. |
A new STRING containing the rightmost n characters of string$.
FUNCTION PBMAIN() AS LONG
LOCAL full$, part$ AS STRING
full$ = "Hello World"
' Basic extraction
part$ = RIGHT$(full$, 5) ' part$ = "World"
PRINT "Last 5 chars: "; part$
' Extract file extension
full$ = "document.txt"
part$ = RIGHT$(full$, 3) ' part$ = "txt"
PRINT "Extension: "; part$
' Right-justified number extraction
full$ = "Amount: 1234"
part$ = RIGHT$(full$, 4) ' part$ = "1234"
PRINT "Numeric part: "; part$
' Count larger than string length
part$ = RIGHT$(full$, 999) ' part$ = "Amount: 1234"
PRINT "Entire string: "; part$
' Count zero
part$ = RIGHT$(full$, 0) ' part$ = ""
PRINT "Empty: ["; part$; "]"
' Practical: extract last 4 digits of account
full$ = "****-****-****-7890"
part$ = RIGHT$(full$, 4) ' part$ = "7890"
PRINT "Last 4: "; part$
FUNCTION = 0
END FUNCTION
pb_cg_emit_right_builtin (pb_codegen.c:2863); Get source length via lstrlenA mov rcx, [rbp - print_ptr_offset] call [__imp_lstrlenA] mov [rbp - written_offset], rax ; source length ; Clamp n to [0, min(source_len, buffer_capacity)] ; Compute start offset = source_len - n mov rdx, [rbp - print_ptr_offset] add rdx, [rbp - written_offset] ; src + source_len sub rdx, [rbp - print_len_offset] ; src + source_len - n ; Copy n bytes + NUL lea rcx, [rbp - asciiz_scratch] mov r8, [rbp - print_len_offset] call [__imp_memcpy] ; NUL-terminate at buffer[n]
lstrlenA to get the source string length dynamically, so it works correctly with runtime-variable strings.src + strlen - n. If n exceeds length, the start pointer moves before the buffer and the copy is clamped.memcpy is used for the copy because the region may not be NUL-terminated at the right boundary while copying.buffer[n] after the copy.