LEFT$ - QB64 Wiki

LEFT$

From QB64 Wiki

Jump to: navigation, search

The LEFT$ string function returns a part of a STRING from the start a set number of places.


Syntax:

LEFT$(stringvalue$, numberofcharacters%)


  • The string value can be any string of ASCII characters as a string variable.
  • If the number of characters exceeds the string length the entire string is returned.
  • Number of characters cannot be a negative value.
  • LEFT$ returns always starts at the first character of the string, even when a space. LTRIM$ can remove leading spaces.


Example 1: Getting the left portion of a string value.

name$ = "Tom Williams" First$ = LEFT$(name$, 3) PRINT First$

Tom


Example 2: A Replace function using LEFT$ and RIGHT$ with INSTR to insert a different length word into an existing string.

text$ = "This is my sentence to change my words." PRINT text$ oldword$ = "my" newword$ = "your" x = Replace(text$, oldword$, newword$) IF x THEN PRINT text$; x END FUNCTION Replace (text$, old$, new$) 'can also be used as a SUB without the count assignment DO find = INSTR(start + 1, text$, old$) 'find location of a word in text IF find THEN count = count + 1 first$ = LEFT$(text$, find - 1) 'text before word including spaces last$ = RIGHT$(text$, LEN(text$) - (find + LEN(old$) - 1)) 'text after word text$ = first$ + new$ + last$ END IF start = find LOOP WHILE find Replace = count 'function returns the number of replaced words. Comment out in SUB END FUNCTION

This is my sentence to change my words. This is your sentence to change your words.

Note: The MID$ statement can only substitute words or sections of the original string length. It cannot change the string length.


See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page