Left$ ( string$,count )

Parameters

string - the string to take characters from

count - how many characters to take from the left

Description

Returns the leftmost characters of a string.

Left( "hello",2 ) is "he". If you ask for more characters than the string holds you simply get the whole string back, so there is no need to check the length first - Left( "hi",9 ) is "hi".

The everyday uses are trimming text to fit and testing a prefix. Cutting a player name down to the width of a high-score column, shortening a filename for a menu, or checking whether a line starts with a comment marker with If Left( line$,1 ) = ";" are all one-liners.

The count must be 0 or more; a negative value raises a runtime error, so clamp anything you calculated. Asking for 0 characters gives an empty string.

See also: Right, Mid, Len, Instr, LSet.

Example

; Left Example
; ------------

; Left$ returns the first n characters of a string.

player$="Roberta the Rocketeer"
Print "Player name: "+player$
Print ""

; Arcade high-score tables keep just three initials
Print "Arcade initials: "+Upper$(Left$(player$,3))

; Cut a long chat message down to a short preview
chat$="Anyone want to trade a health pack for two keys?"
Print "Chat preview:    "+Left$(chat$,20)+"..."

; Asking for more characters than exist returns the whole string
Print "Left$(name,99):  "+Left$(player$,99)

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

End

Index