Blitz3D+ Command Reference

Right$ ( string$,count )

Parameters

string - the string to take characters from

count - how many characters to take from the right

Description

Returns the rightmost characters of a string.

Right( "hello",2 ) is "lo". As with Left, asking for more characters than the string holds just gives you the whole string back, so no length check is needed.

The obvious use is testing a suffix - checking a file extension with If Right( Lower( f$ ),4 ) = ".png", for instance. It is also the quick way to shorten a fixed-width value from one of the formatting commands: Hex always returns eight digits and Bin thirty-two, so Right( Hex( c ),6 ) gives you the six-digit colour you actually wanted.

The count must be 0 or more; a negative value raises a runtime error.

See also: Left, Mid, Len, RSet, Hex.

Example

; Right Example
; -------------

; Right$ returns the last n characters of a string.

player_id$="PLAYER-2026-0042"
Print "Player id: "+player_id$
Print "Last 4 characters: "+Right$(player_id$,4)
Print ""

; Check a file extension before loading a level
file$="dungeon_level.map"
If Right$(file$,4)=".map" Then Print "'"+file$+"' looks like a level file"

; Classic zero-padded score: pad with zeros, keep the right end
score=42
Print "Score display: "+Right$("00000000"+score,8)

; Asking for more characters than exist returns the whole string
Print "Right$(id,99): "+Right$(player_id$,99)

Print ""
Print "Press any key to close the example"
WaitKey

End

Index