Positron Compiler Documentation

Mid$

Positron16 · Revision 1.1.3.5 · PDF pages 367–368

Destination String = Mid$ (Source String, Position within String, Amount of characters)

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

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. 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. Position within String can be any valid variable type, expression or constant value, that signifies the position within the Source String from which to start extracting characters. 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. 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 and should not exceed 255 which is the maximum allowable length of a String variable.

' Copy 5 characters from position 4 of SourceString into DestString

  Device = 24HJ128GP502             ' Select the device to compile for
  Declare Xtal = 16

  Dim SourceString as String * 20   ' Create a String of 20 characters
  Dim DestString as String * 20   ' Create another String

  SourceString = "Hello World"      ' Load the source string with characters
' Copy 5 characters from the source string into the destination string
  DestString = Mid$(SourceString, 4, 5)
  Print DestString      ' Display the result, which will be "lo Wo"
' Copy 5 chars from position 4 of a Quoted Character String into DestString

  Device = 24HJ128GP502             ' Select the device to compile for
  Declare Xtal = 16

  Dim DestString as String * 20   ' Create a String of 20 characters

' Copy 5 characters from the quoted string into the destination string
  DestString = Mid$("Hello World", 4, 5)
  Print DestString      ' Display the result, which will be " lo Wo "

The Source String can also be a Byte, Word, 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.

View original PDF page 367Open page in PDFOriginal PDF page 367
' Copy 5 chars from position 4 of SourceString to DestString with a pointer
' to SourceString

  Device = 24HJ128GP502             ' Select the device to compile for
  Declare Xtal = 16

  Dim SourceString as String * 20   ' Create a String of 20 characters
  Dim DestString as String * 20   ' Create another String
' 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 = Mid$(StringAddr, 4, 5)
  Print DestString      ' Display the result, which will be " lo Wo "

A third possibility for Source String is a Label name, in which case a null terminated Quoted String of Characters is read from code memory.

' Copy 5 characters from position 4 of a code memory string into DestString

  Device = 24HJ128GP502             ' Select the device to compile for
  Declare Xtal = 16

  Dim DestString as String * 20   ' Create a String of 20 characters
' Create a null terminated string of characters in code memory
  Dim Source as Code = Hello World", 0

' Copy 5 characters from label Source into the destination string
  DestString = Mid$(Source, 4, 5)
  Print DestString               ' Display the result, which will be "LO WO"

See also : Creating and using Strings, Creating and using code memory strings, Len, Left$, Right$, Str$, ToLower, ToUpper, AddressOf .

View original PDF page 368Open page in PDFOriginal PDF page 368