LEFT$: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The LEFT$ string function returns a number of characters from the left of a STRING. {{PageSyntax}} : LEFT$({{Parameter|stringValue$}}, {{Parameter|numberOfCharacters%}}) {{Parameters}} * {{Parameter|stringValue$}} can be any STRING literal or variable. * {{Parameter|numberOfCharacters%}} INTEGER determines the number of characters to return from left of string. {{PageDescription}} * If the number of characters exceeds the string length the entir...")
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 6: Line 6:




{{Parameters}}
{{PageParameters}}
* {{Parameter|stringValue$}} can be any [[STRING]] literal or variable.
* {{Parameter|stringValue$}} can be any [[STRING]] literal or variable.
* {{Parameter|numberOfCharacters%}} [[INTEGER]] determines the number of characters to return from left of string.
* {{Parameter|numberOfCharacters%}} [[INTEGER]] determines the number of characters to return from left of string.
Line 18: Line 18:


{{PageExamples}}
{{PageExamples}}
''Example 1:'' Getting the left portion of a string value.  
''Example 1:'' Getting the left portion of a string value.
{{CodeStart}} '' ''
{{CodeStart}}
name$ = "Tom Williams"
name$ = "Tom Williams"


First$ = LEFT$(name$, 3)
First$ = LEFT$(name$, 3)


PRINT First$ '' ''
PRINT First$
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}Tom {{OutputEnd}}
{{OutputStart}}Tom {{OutputEnd}}




''Example 2:'' A replace function using LEFT$ and [[RIGHT$]] with [[INSTR]] to insert a different length word into an existing string.  
''Example 2:'' A replace function using LEFT$ and [[RIGHT$]] with [[INSTR]] to insert a different length word into an existing string.
{{CodeStart}} '' ''
{{CodeStart}}
text$ = "This is my sentence to change my words."
text$ = "This is my sentence to change my words."
{{Cl|PRINT}} text$
{{Cl|PRINT}} text$
oldword$ = "my"  
oldword$ = "my"
newword$ = "your"
newword$ = "your"


x = Replace(text$, oldword$, newword$)  
x = Replace(text$, oldword$, newword$)
{{Cl|IF...THEN|IF}} x {{Cl|THEN}} {{Cl|PRINT}} text$; x
{{Cl|IF...THEN|IF}} x {{Cl|THEN}} {{Cl|PRINT}} text$; x


Line 53: Line 53:
{{Cl|LOOP}} {{Cl|WHILE}} find
{{Cl|LOOP}} {{Cl|WHILE}} find
Replace = count 'function returns the number of replaced words. Comment out in SUB
Replace = count 'function returns the number of replaced words. Comment out in SUB
{{Cl|END FUNCTION}} '' ''
{{Cl|END FUNCTION}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}This is my sentence to change my words.
{{OutputStart}}This is my sentence to change my words.
This is your sentence to change your words.{{OutputEnd}}
This is your sentence to change your words.{{OutputEnd}}
: ''Note:'' The [[MID$ (statement)|MID$]] statement can only substitute words or sections of the original string length. It cannot change the string length.
: ''Note:'' The [[MID$]] statement can only substitute words or sections of the original string length. It cannot change the string length.




{{PageSeeAlso}}
{{PageSeeAlso}}
* [[RIGHT$]], [[MID$]]
* [[RIGHT$]], [[MID$ (function)]]
* [[LTRIM$]], [[RTRIM$]]
* [[LTRIM$]], [[RTRIM$]]
* [[MID$ (statement)]]
* [[INSTR]], [[LEN]]
* [[INSTR]], [[LEN]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 00:38, 26 February 2023

The LEFT$ string function returns a number of characters from the left of a STRING.


Syntax

LEFT$(stringValue$, numberOfCharacters%)


Parameters

  • stringValue$ can be any STRING literal or variable.
  • numberOfCharacters% INTEGER determines the number of characters to return from left of string.


Description

  • If the number of characters exceeds the string length the entire string is returned. Use LEN to determine a string's length.
  • LEFT$ returns always start at the first character of the string, even if it's a space. LTRIM$ can remove leading spaces.
  • numberOfCharacters% cannot be a negative value.


Examples

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:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link