PIC32 Strings

Blank

mid$()
left$()
right$()
instr()
split()
len()
asc()
str$()
chr$()
hex$()
format$()

Paragraph text.

mid$()
mid$(str$,start,length)
a$=mid$(“fred”,1,2)
b$=mid$(“fred”,5,1)
c$=mid$(“fred”,1,20)

This will extract a string from str$, starting at start for a given length. The size of the extracted string will normally be length. The first letter is 1 (not 0).

In the above examples, a$=”fr” as two characters have been extracted from “fred” starting at the first character. In the second example b$=”” i.e. nothing, as the start value of 5 is greater than the number of charterers in “fred”. The variable c$=”fred” as mid$ will return at least the length and if the length is greater then the number of characters then it returns all of the characters.

left$
left$(str$,length)
a$=left$(“fred”, 2)
b$=left$(“fred”,10)

Left$ always starts with the first character and returns the number of character requested and so a$=”fr” In the situation where the length exceeds the number of characters available, the whole str$ is returned.

right$
right$(str$,length)
a$=right$(“fred”, 2)
b$=right$(“fred”,10)

This is a similar statement to left$ except the last character is the start character and the length is working backwards from the end of str$. So a$=”ed” Again if the length exceeds the number of characters the whole str$ is returned, thus b$=”fred”.

instr()
instr(x$[;n],y$)
a=instr(x$,y$)
b=instr(x$;5,y$)
c=instr(“abcdefgh”,letter$)

In String: searches for the first occurrence of string y$ in string x$. Returns the offset into the string x$ starting at 1. If there is no match 0 is returned. An optional offset (starting at 1) can be added to the string to be searched, if the offset is greater than the search string or less than 1, 0 will be returned and the error() indicator set.

As an example if x$="hello World" and y$="l" then a=3 and b=10 in the above examples. The first example picks up the first 'l' in Hello and the second example picks up the 'l' in World because of the offset of 5.

split()
n=split(<input$><delimiter&><output$[]>)
a=split(“hello fred bob”,’ ‘,a$[2]

This word is used tor 'tokenising' a sting into separate parts. The parts are delimited by the given delimiter charater and put into the single dimension string array provided.

Example:

function s1
dim a$[6:10], source$="hello fred and bob"
dim i
    x=split(source$,' ',a$[1])
    print "x=";x
    for i = 1 to 6
        print "i=";i, a$[i]
    next
endf

The output from this is:

x=4
i=1    hello
i=2    fred
i=3    and
i=4    bob
i=5   
i=6

Notes:

  1. ‘split’ returns the number of successful substrings stored. If there was an error it returns 0. WARNING: There are two possible silent errors generated from this so check ‘error()’.
  2. The source string maximum length is 126 characters
  3. The delimiter is a character type with valid values from 1 to 255
  4. The delimiter is not stored in the substrings.
  5. The storage is a SINGLE dimension STRING array, the index given will be the starting index for the storage.

There are two possible errors that are not reported to the screen. These occur when either the substring will not fit into the string length of the individual slot and if the array size is exceeded. See also the section for error().

len()
len(str$)
a=len(“fred”)
b=len(“”)

This will return the length of the string as an integer value. In the above a=4 and b=0.

asc()
asc(str$,position)
a=asc(“fred”,2)
b=asc(“fred”,10

Asc will return as an integer value the ASCII value of the character in the string pointed to by position, the first character is 1 (not 0). In the example a=114 because the ASCII value of ‘r’ is 114 and b=0. In the latter case, position 10 is beyond the length of “fred” and so this special case can detect when the end of the string has been reached (or gone beyond). Zero is in fact an ASCII value but it is called NULL so this is fitting.

str$()
str$(77)
a$=str$(12)
b$=str$(x#)

Converts a number to a string, although most of the time this will not be needed as automatic conversion takes place. In the above examples a$=”12” and if x#=31.22 then b$=”31.22”

chr$()
str$(77)
a$=chr$(44)

Converts an integer into the ASCII string equivalent. In the above example a$=”B”. If the value is outside the printable range <32 or > 128 then a ‘.’ is returned.

hex$()
hex$(77)
a$=hex$(12)
b$=hex$(x)
print hex$(value)

Converts a decimal number to a hex string so in the first example a$=”c” as 0xc is the same as decimal 12. If this is just for printing purposes then it may be more convenient to use a HEX variable, for example x% or fred%. See variables.

format$()
format$(“format”,<expression>)
a$=format$(“#.##”, a#+6)
print format$(“#.####”,x%)

Converts a numeric value into a formatted string. The output of this depends on the type of variable.