Small Basic: Text Basics

Small Basic: Text Basics

This article covers the basics of text manipulation in Small Basic using the Text object.


Text is a series of characters, often called a string in computing.  A string can be a constant (or literal) enclosed within a set of double quotes, or a variable can be assigned to hold a string.

txt = "Hello World"

Above, txt is a variable that contains the string literal "Hello World".

As stated, the string contains a set or characters, in the example above there are 11 characters, 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l' and 'd'. 

A string can also be a series of numbers such as "3.14159", or even contain special characters like backspace, newline, '♥' or even a system beep.  These are not to be confused with the different representations of the characters that different fonts can provide, including different language fonts like the symbol font.

Each character is identified internally by a 'character code' or number, often called ASCII or UNICODE number.

We often want to form one string from others or query a string to see what it contains.

Text methods

The following is a brief summary of the Small Basic Text object methods with some simple examples.


Returns the number or characters in the input string.

txt = "Hello World"
TextWindow.WriteLine(txt+" contains "+Text.GetLength(txt)+" characters")

The empty string "" has a special meaning in Small Basic.  It represents a variable that has not been set, and in fact an array or one of its elements is deleted by setting it to "".  We cannot therefore have an array element that is the empty string "".


Joins or concatenates two strings.  We can also use + to join two strings in general.  However, if the strings are actually numbers, then + would add them numerically rather than join the strings.


Case Conversion

This is very straight forward, simply creates a copy of the input string converting the case. 

This can be very useful for comparing the equivalence of strings in a case insenstive way.  If we convert both to the same case (say lower) and test if they are equal then the case of the original things is effectively ignored.

TextWindow.WriteLine("Enter a name")
inputName = TextWindow.Read()
If (Text.ConvertToLowerCase(inputName) = "john") Then
  TextWindow.WriteLine("Hello "+inputName)


Create a copy of the input string, with all characters converted to lower case.


Create a copy of the input string, with all characters converted to upper (or capital) case.

Character Codes

Character codes can be useful to:

  • Quickly check for certain ranges of characters, e.g. a capital letter.
  • Add special characters like new line or backspace.
  • Provide a unique numerical value for a character, perhaps for a code method.
For i = 0 To 127
  TextWindow.CursorLeft = 0
  TextWindow.CursorLeft = 4
TextWindow.WriteLine("Enter a phrase")
input = TextWindow.Read()
capitals = ""
For i = 1 To Text.GetLength(input)
  char = Text.GetSubText(input,i,1)
  charCode = Text.GetCharacterCode(char)
  If (charCode >= 65 And charCode <= 90) Then
    capitals = capitals+char
TextWindow.WriteLine("The capital letters in your input are")

The special character codes for a carriage return (10) and line feed (13) can be useful.  These terms relate to manual typewriters; they are sometimes used separately and sometimes combine to form a newline.

CR = Text.GetCharacter(10)
LF = Text.GetCharacter(13)
mlTextBox = Controls.AddMultiLineTextBox(0,0)


Get a character from an input character code.


Get the character code for an input character.

Sub-text manipulation

The following commands manipulate sub-strings within larger strings.


Finds if a string ends with a set of characters, returns "True" or "False".

If (Text.EndsWith(fileName,".txt")) Then
  'We have found a txt file

Note that "True" and "False" are special strings in Small Basic that represent a true or false state and can be used directly in If or While statements.

'An infinite loop
While ("True")


Finds if a string starts with a set of characters, returns "True" or "False".

The final set of Text methods allow us to find and manipulate sub strings.

'A simple find and replace
txt = "This is my test text, it contains text that has 3 instances of the word text."
txtCopy = txt ' A working copy
find = "text" 'The text to find
replace = "TEXT" 'The text to replace with
result = "" 'The result of the find and replace
pos = Text.GetIndexOf(txtCopy,find) ' The next occurence of our search text
While (pos > 0)
  result = result+Text.GetSubText(txtCopy,1,pos-1) 'The text before our search text
  result = result+replace 'Add the replace text
  txtCopy = Text.GetSubTextToEnd(txtCopy,pos+Text.GetLength(find)) 'The text after our search text
  pos = Text.GetIndexOf(txtCopy,find) 'The next occurence of our next search text
result = result+txtCopy 'The final text after our search text (if any).


Find the index (or character from the start) of the start of one string in another.  This returns 0, if the string is not found.


This gets a sub text of the input string starting at a given index and extracting a defined number of characters.


This gets a sub text of the input string starting at a given index and extracting all of the string from this point to the end.


The checks if a sub string is present anywhere within the input string, returns "True" or "False".  This is similar to GetIndexOf returning a value > 0.

Leave a Comment
  • Please add 1 and 8 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
  • Carsten Siemens edited Revision 1. Comment: fixed typo

  • Excellent !

    Very useful for learning and teaching

  • Very nice! you explained things very well. a beginner should have no problem understanding this!

Page 1 of 1 (3 items)