Q(uick)BASIC Function: RIGHT$

Quick View

RIGHT$

A string function that returns the rightmost n characters of a string

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • LEFT$(stringexpression$,n%)
  • RIGHT$(stringexpression$,n%)
Description/Parameter(s)
stringexpression$ Any string expression.
n% The number of characters to return, beginning with the leftmost or rightmost string character.
Example
a$ = "Microsoft QBasic" PRINT LEFT$(a$, 5) 'Output is: Micro PRINT RIGHT$(a$, 5) 'Output is: Basic

See also:

Syntax
  • RIGHT$(stringexpression,n)
Description/Parameter(s)
  • stringexpression is a string constant, string variable, or string expression
  • n, a numeric expression which must have a value between 0 and 32,767, specifies how many right-most characters the function should return. If n is zero, the null string (a zero-length string) is returned.

The argument stringexpression can be any string variable, string constant, or string expression. If n is equal to the number of characters in the argument stringexpression, then the RIGHT$ function returns stringexpression. If n = 0, RIGHT$ returns the null string (length zero).

Example

This example converts names input in the form "Firstname [Middlename] Lastname" to the form "Lastname, Firstname [Middlename]".

CLS ' Clear screen LINE INPUT "Name: "; Nm$ I = 1 : Sppos = 0 DO WHILE I > 0 I = INSTR(Sppos + 1, Nm$, " ") 'Get position of next space. IF I > 0 THEN Sppos = I LOOP 'SPPOS now points to the position of the last space. IF Sppos = 0 THEN PRINT Nm$ 'Only a last name was input. ELSE 'Everything after last space. Lastname$ = RIGHT$(Nm$, LEN(Nm$) - Sppos) 'Everything before last space. Firstname$ = LEFT$(Nm$, Sppos - 1) PRINT Lastname$ ", " Firstname$ END IF END
Syntax
  • RIGHT$(stringexpression$,n%)
Description/Parameter(s)
stringexpression$ Any string variable, string constant, or string expression.
n% A numeric expression whose value is between 0 and 32,767, inclusive, indicating how many characters are to be returned.

Usage Notes

  • If n% is 0, the null string (length 0) is returned.
  • If n% is greater than or equal to the number of characters in stringexpression$, the entire string is returned.
  • To find the number of characters in stringexpression$, use LEN(stringexpression$).
Example

This example uses the RIGHT$ function to convert names input in the form "Firstname [Middlename] Lastname" to the form "Lastname, Firstname [Middlename]".

CLS 'Clear screen. LINE INPUT "Name: "; Nm$ I = 1: Sppos = 0 DO WHILE I > 0 I = INSTR(Sppos + 1, Nm$, " ") 'Get position of next space. IF I > 0 THEN Sppos = I LOOP 'Sppos now points to the position of the last space. IF Sppos = 0 THEN PRINT Nm$ 'Only a last name was input. ELSE 'Everything after last space. Lastname$ = RIGHT$(Nm$, LEN(Nm$) - Sppos) 'Everything before last space. Firstname$ = LEFT$(Nm$, Sppos - 1) PRINT Lastname$; ", "; Firstname$ END IF END