Mid$
Syntax
Destination String = Mid$ (Source String, Position within String, Amount of characters)
Overview
Extract n amount of characters from a source string beginning at n characters from the left, 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. 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. 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 position 4 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 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)
HRsoutLn DestString ' Display the result, which will be "Lo Wo"
Example 2
' Copy 5 chars from position 4 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 of 20 characters
'
' Copy 5 characters from the quoted string into the destination string
'
DestString = Mid$("Hello World", 4, 5)
HRsoutLn DestString ' Display the result, which will be "Lo Wo"
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 chars from position 4 of SourceString to DestString with 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 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)
HRsoutLn 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 a Cdata table.
Example 4
' Copy 5 characters from position 4 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 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 = Mid$(Source, 4, 5)
HRsoutLn DestString ' Display the result, which will be "Lo Wo"
See also : Creating and using Strings, Creating and using Virtual Strings with Cdata, Cdata, Len, Left$, Right$, Str$, ToLower, ToUpper, AddressOf.

