Amos Professional Manual  Contents  Index

String Functions


MID$
function: return a number of characters from the middle of a string
destination$=Mid$(source$,offset,number)
Mid$(destination$,offset,number)=source$

Similarly, the MID$ function returns characters from the middle of a string, with the first number specified in brackets setting the offset from the start of the string and the second number setting how many characters are to be fetched. If the number of characters to be fetched is omitted from your instruction, then the characters will be read right up to the end of the string being examined. Here are some examples:

E> Print Mid$("AMOS Professional",6)
   Print Mid$("AMOS Professional",6,4)

E> A$="AMOS Professional ***"
   Mid$(A$,19)="Basic"
   Print A$
   Mid$(A$,19,3)="Mag"
   Print A$

Finding characters in a string
It is often necessary to search through a mass of data for a particular reference, in other words, to search through strings for individual characters or sub-strings. Similarly, you may wish to write an adventure game where lines of text must be broken down into individual commands.

INSTR
function: search for occurrences of one string within another string
x=Instr(host$,guest$)
x=Instr(host$,guest$,start of search position)

INSTR allows you to search for all instances of one string inside another. In the following examples, the "host" strings are searched for the first occurrence of the "guest" strings you are seeking. If the relevant string is found, its location will be reported in the form of the number of characters from the left-hand side of the host string. If the search is unsuccessful, a result of zero will be given.

E> Print Instr("AMOS Professional","AMOS")
   Print Instr("AMOS Professional","O")
   Print Instr("AMOS Professional","o")
   Print Instr("AMOS Professional","Provisional")

E> Do
    Input "Type in a host string:";H$
    Input "Type in a guest string to be found:";G$
    X=Instr(H$,G$)
    If X=0 Then Print G$;" Not found"
Back    Next
05.02.02