BASIC String functions

From DenialWIKI
Jump to navigation Jump to search

ASC

Converts a single string character to it's numeric PETSCII equivalent.

Example:

A$="A"

READY.
PRINT ASC(A$)
 65
READY.


CHR$

Converts a numeric PETSCII value to it's character equivalent

Example:

A=65

READY.
PRINT CHR$(A)
A
READY.


MID$

Extracts the middle of a string. It is passed three parameters.

  1. The string
  2. where to start
  3. How long we want to cut

Example:

Therefore if the String is "HELLO CAT" and we want to remove the word CAT we would write this

PRINT MID$("HELLO CAT",7,3)
CAT

READY.

Since the C is the 7th letter in and CAT is three letters long.


LEFT$

Extracts the left side of a string. It is passed two parameters.

  1. The string
  2. The rightmost character to go to

Example:

If the String is "VIC20" and we want to extract VIC we would write this

PRINT LEFT$("VIC20",3)
VIC

READY.

V is the first letter and C is the third.


RIGHT$

Extracts the right side of a string. It is passed two parameters.

  1. The String
  2. How many characters from the right end of the string to extract

Example:

PRINT RIGHT$("1234",3)
234

READY.

And if we change the second parameter to 2

PRINT RIGHT$("1234",2)
34

READY.

If we want to extract the "20" out of "VIC20" we count from the end of the string. "0" is the 1st, "2" is the 2nd from the end.

PRINT RIGHT$("VIC20",2)
20

READY. 

Note if you use both left$ and right$ the second parameter is different.

PRINT LEFT$("OVERTIME",2);RIGHT$("OVERTIME",6)
OVERTIME

READY.

left$ counts from the left and right$ counts from the right.


LEN

Returns the length of a string. Or you could see it as the number of characters in a string. It is passed 1 parameter.

  1. the string

Example:

If the string is "AVERYLONGSTRING" and you wanted to know its length you could type this

PRINT LEN("AVERYLONGSTRING")
 15

READY.



STR$

Converts a number into a string

Example:

A=5.5
B=7.7
A$=STR$(A) + STR$(B)
PRINT A$
 5.5 7.7

READY.



SPC

Repeats the space character X times. Where X is the parameter passed.

PRINT SPC(5) 

will print 5 space characters.

Note you cannot pass the output of SPC to a string

A$=SPC(5)

?SYNTAX
 ERROR
READY.