Positron Compiler Documentation

Left$

Source: Positron8 Compiler User Manual, PDF page 363

Syntax

Destination String = Left$ (Source String, Amount of characters)

Overview

Extract n amount of characters from the left of a source string and copy them into a destination string.

Parameters

Source String can be a String variable, or a Quoted String of Characters. See below for more variable types that can be used for Source String. Amount of characters can be any valid variable type, expression or constant value, that signifies the amount of characters to extract from the left of the Source String. Values start at 1 for the leftmost part of the string and should not exceed 255 which is the maximum allowable length of a String variable. Destination String can only be a String variable, and should be large enough to hold the correct amount of characters extracted from the Source String.

Example 1.

' Copy 5 characters from the left of SourceString into DestString
  Device = 18F26K40
                                       ' A suitable device for Strings
  Declare Xtal = 16
                                ' Tell the compiler the device is operating at 16MHz
  Declare Hserial_Baud = 9600
  Dim SourceString as String * 20
                                       ' Create a String capable of 20 characters
  Dim DestString as String * 20
                                       ' Create another String for 20 characters
  SourceString = "Hello World"
                                       ' Load the source string with characters
' Copy 5 characters from the source string into the destination string
  DestString = Left$ (SourceString, 5)
  HRsoutLn DestString
                                       ' Display the result, which will be "HELLO"

Example 2.

' Copy 5 chars from the left of a Quoted Character String into DestString
  Device = 18F26K40
                                       ' A suitable device for Strings
  Declare Xtal = 16
                                ' Tell the compiler the device is operating at 16MHz
  Declare Hserial_Baud = 9600
  Dim DestString as String * 20
                                       ' Create a String capable of 20 characters
' Copy 5 characters from the quoted string into the destination string
  DestString = Left$("Hello World", 5)
  HRsoutLn DestString
                                       ' Display the result, which will be "HELLO"

The Source String can also be a Byte, Word, Long, Dword, Float or Array variable, in which case the value contained within the variable is used as a pointer to the start of the Source String's address in RAM.

Example 3.

' Copy 5 characters from the left of SourceString into DestString using a
' pointer to SourceString
  Device = 18F26K40
                                       ' A suitable device for Strings
  Declare Xtal = 16
                                ' Tell the compiler the device is operating at 16MHz
  Declare Hserial_Baud = 9600
  Dim SourceString as String * 20
                                       ' Create a String capable of 20 characters
  Dim DestString as String * 20
                                       ' Create another String for 20 characters
' Create a Word variable to hold the address of SourceString
  Dim StringAddr as Word
  SourceString = "Hello World"
                                       ' Load the source string with characters
' Locate the start address of SourceString in RAM
  StringAddr = AddressOf(SourceString)
' Copy 5 characters from the source string into the destination string
  DestString = Left$(StringAddr, 5)
  HRsoutLn DestString
                                       ' Display the result, which will be "HELLO"

A third possibility for Source String is a label name, in which case a null terminated Quoted String of Characters is read from a Cdata table or a Dim As Flash table.

Example 4.

' Copy 5 characters from the left of a Flash memory table into DestString
  Device = 18F26K40
                                     ' A suitable device for Strings
  Declare Xtal = 16
                                ' Tell the compiler the device is operating at 16MHz
  Declare Hserial_Baud = 9600
  Dim DestString as String * 20
                                     ' Create a String capable of 20 characters
'
' Create a null terminated string of characters in flash memory
'
  Dim Source as Code = "Hello World", 0
'
' Copy 5 characters from label Source into the destination string
'
  DestString = Left$(Source, 5)
  HRsoutLn DestString
                                     ' Display the result, which will be "HELLO"

See also : Creating and using Strings, Creating and using Virtual Strings with

Cdata, Cdata, Len, Mid$, Right$, Str$, ToLower, ToUpper , AddressOf.