www.riscos.com Technical Support:
BBC BASIC Reference Manual


String variables

String variables may be used to store strings of characters, constituting words and phrases. This chapter shows you how to assign values to a string variable, and describes several useful operations you can perform on strings in BASIC; such as splitting a string and joining two or more strings together.

Assigning values to string variables

Each string can be up to 255 characters long. The following gives some examples of strings:

day$ = "Monday"
Date$ = "29th February"
space$ = " "
Address$ = "10 Downing Street, London"
Age$ = "21"

Note that the variable Age$ is assigned a string containing the two characters 2 and 1, and not the number 21. So, if you type

Real_Age$ = 21 * 2

the result will not be "42" because BASIC cannot do arithmetic with strings. Instead, the error message:

Type mismatch: string needed

appears on the screen, indicating that only a string expression can be assigned to a string variable. A type mismatch error can also be caused by an attempt to multiply strings, as in:

total$ = "12"*"32"

You should note that the 'null' string "" is valid. This is a string containing zero characters. In comparisons, it is less than any other string (except, of course, another null string).

In order to obtain a double quotation character, ", in a string, you use two of them adjacent to each other. For example, to print the text A"here, you would use:

PRINT "A""here"

Joining strings together

Two strings may be joined together, or, more correctly speaking, concatenated. The + operator is used to indicate this:

10 Road$ = "Downing Street"
20 City$ = "London"
30 PRINT Road$ + " " + City$

Typing RUN produces the following:

Downing Street London

The += operator can also be used, and as the following program shows, produces the same output as +.

10 Address$  = "Downing Street"
20 Address$ += " "
30 Address$ += "London"
40 PRINT Address$

Note, however, that the -= operator is meaningless when applied to strings and produces an error message.

Splitting strings

As well as joining two strings together, BASIC can split a string into smaller sequences of characters. Three functions are provided for doing this.

  • LEFT$(A$,n) which gives the first (lefthand end) n characters of a string.
  • RIGHT$(A$,n) which gives the last (righthand end) n characters of a string.
  • MID$(A$,m,n) which gives n characters from the middle, beginning at the mth character.

For example,





10 title$ = "Moonlight Sonata"
20 left_of_string$ = LEFT$(title$,4)
30 right_of_string$ = RIGHT$(title$,6)
40 middle_of_string$ = MID$(title$,5,9)
50 PRINT left_of_string$
60 PRINT right_of_string$
70 PRINT middle_of_string$

produces the following when run:

light Son

Each of these functions has a convenient shorthand form:

  • LEFT$(A$) gives all but the last character of the string
  • RIGHT$(A$) gives the last character of the string
  • MID$(A$,m) gives all the characters from the mth to the last.

For example:

10 PRINT LEFT$("Hello")
20 PRINT RIGHT$("Hello")
30 PRINT MID$("Hello",3)

produces the following:


LEFT$, RIGHT$ and MID$ may be used to replace part of a string. In each case the number of new characters equals the number of characters being replaced, and the string stays the same length. The number of characters being changed can be determined by the length of the replacement string. Thus:

10 A$ = "Hello there."
20 MID$(A$,7) = "Susan"
40 LEFT$(A$) = "Howdy"
60 RIGHT$(A$) = "!"


Hello Susan.
Howdy Susan.
Howdy Susan!

Alternatively, you can give the maximum number of characters to be replaced. Then, if the length of the replacement string is less than the given value, all of it is used. Otherwise only the first designated number of characters have an effect. For example,

20 RIGHT$(A$,3) = "KL"
40 LEFT$(A$,4) = "MNOPQR"
60 MID$(A$,4,3) = "STUVW"


Other keywords for manipulating strings

There are also BASIC keywords to:

  • produce a long string consisting of multiple copies of a shorter string
  • find the length of a string
  • determine whether one string is contained within the other.

These keywords are:

  • STRING$(n,A$), which returns a string consisting of n copies of A$.
  • LEN(A$), which gives the length of string A$.
  • INSTR(A$,B$), which looks for the string B$ within the string A$ and returns the position of the first place where it is found.

For example,

PRINT STRING$(20,"+-")

produces the output:


The statement PRINT LEN("PAUL") prints the number 4 and

A$ = "Great Britain"

produces the result 13. Note that the space is treated like any other character.

A$ = "Great Britain"

prints 9 because the string it is contained in Great Britain at the ninth character position. If the substring in the INSTR function is not present in the first string, then 0 is returned. Note also that you can start the search for the substring at any position, not just from the start of the substring. This is done by specifying a third parameter, so that for example,

PRINT INSTR("'ello 'ello","'ello",2)

will print 7, since the first occurrence of the substring will be skipped.

You can use the relational operators >, =, <= etc, to compare two strings. See the chapter entitled Control statements for details.

How characters are represented

Every character and symbol which can be reproduced on the screen is represented within the computer by a number in the range 0 to 255. The system used to assign numbers to characters and symbols is known as ISO-8859. This is an extension of the very popular ASCII (American Standard Code for Information Interchange) code, which only applies to characters between 0 and 127. We shall use ASCII as a general term for character codes. It is wise to follow such a standard so that different computers can all understand the same numerical alphabet.

BASIC provides a pair of functions for converting characters to their ASCII number-codes and back again. These are:

  • ASC(a$), which gives the ASCII code of the first character of a string.
  • CHR$(n), which gives the one-character string whose ASCII code is n.

Converting between strings and numbers

There are three keywords which convert between strings and numbers:

  • VAL(A$), which converts a string of digits A$ into a number.
  • STR$(n), which converts the number n into a string.
  • EVAL(A$), which evaluates the string A$ as though it were a BASIC expression.

VAL returns the value of a string, up to the first non-numeric character.

For example:

PRINT VAL("10to10")

prints the value 10, since all the characters after the t are ignored. The string may, however, begin with a + or -. Thus,

number = VAL("-5")

assigns the value -5 to number. If, however, the string does not start with a digit or a plus or minus sign, VAL returns 0.


EVAL however, considers the whole string as an expression, allowing operators and variable names to occur within it. Variables must be assigned values beforehand.

10 radius = 5
20 area = EVAL("PI*radius^2")
30 PRINT area

When this program is run the value printed is 78.5398163, which is the value PI (3.141592653) multiplied by 5 squared.


STR$ performs the opposite conversion to the above two functions. It takes the number given and returns a string containing the digits in the number.

For example,

10 A = 45
20 B = 30.5
30 A$ = STR$(A)
40 B$ = STR$(B)
50 PRINT A + B
60 PRINT A$ + B$

produces the following when it is run:


BBC BASIC can express numbers in base 16 (hexadecimal) as well as base 10 (decimal). This is useful for dealing with certain types of integer. The chapter entitled Bases explains more about the various ways in which bases can be used. STR$~x gives the hexadecimal string representation of x. Thus

10 A = 45
20 A$ = STR$~(A)



because 2D is the hexadecimal version of the decimal number 45.

This edition Copyright © 3QD Developments Ltd 2015
Last Edit: Tue,03 Nov 2015