Categories: All Variables and Constants  String manipulation  Builtin functions  Input and output  Arrays  Conditional logic  Repetition / Iteration  Functions / Procedures 

Show:

This resource is designed as a quick revision guide and must not be used by students during a NEA where internet access is not allowed. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.

String manipulation

Any text in a program is stored as a string data type. Strings are made up of characters, which are individual letters, numbers or symbols.

Think of a piece of string hanging like a washing line outside. Pegged to that piece of string is each character in order.

Manipulating strings means changing or working with the characters inside a string.

Top Tips:

  • Concatenating strings means joining strings together
    The string "there" concatenated to the string "hello " becomes the string "hello there"
  • Finding a substring means ignoring part of the string and keeping the part that you want.
    "it" is a substring of "biscuit": it's the 6th and 7th characters (counting from 1).
  • Most programming languages count from 0 rather than 1 when getting a substring
    "it" is a substring of "biscuit": it's the 5th and 6th characters (counting from 0)
String variables
    

A string is some text such as "Hello" or "Area 51"

Variables let you store data.

String variables let you store text.

In this example a variable called name is set to the string value of "Bob"

STRING name SET name TO "Bob"
Dim name As String name = "Bob"
name = "Bob"
name = "Bob"
name is string set name = "Bob"
String constants
    

A string is some text such as "Hello" or "Area 51"

Constants store a value that is set once and then never changes.

String constants let you give some text data a name so that you can use it to make your code easier to read or set options for your code that the user wont be able to change.

In this example, a constant called ERROR_MESSAGE is set to the string value of "Don't panic!"

CONST STRING ERROR_MESSAGE SET ERROR_MESSAGE TO "Don't panic!"
Const ERROR_MESSAGE As String = "Don't panic!"
ERROR_MESSAGE = "Don't panic!"
ERROR_MESSAGE = "Don't panic!"
{ no mention of constants in pseudocode reference } ERROR_MESSAGE is string set ERROR_MESSAGE = "Don't panic!"
Concatenating strings
   

A string is some text such as "Hello"

Concatenating means joining together or adding / appending on to the end.

Concatenating strings means joining two strings together.

In this example, two strings (first_name and last_name) are concatenated (joined together) and saved in another string variable called full_name with a space in between them.

STRING first_name STRING last_name STRING full_name SET first_name TO "Bob" SET last_name TO "Builder" SET full_name TO first_name & " " & last_name
Dim first_name As String Dim last_name As String Dim full_name As String first_name = "Bob" last_name = "Builder" full_name = first_name & " " & last_name
first_name = "Bob" last_name = "Builder" full_name = first_name + " " + last_name
first_name = "Bob" last_name = "Builder" full_name = first_name + " " + last_name
firstname is string lastname is string fullname is string set firstname = "Bob" set lastname = "Builder" set fullname = firstname + " " + lastname
Length of a string
    

A string is some text such as "Hello"

The length of a string means how many characters (including spaces, numbers and punctuation) there are in that string

This example createa a string called name and sets it to "Bob". It counts how many characters are in that string and stores the answer (3) into a variable called l

STRING name SET name TO "Bob" INT l SET l TO LENGTH(name)
Dim name As String name = "bob" Dim l As Integer l = LEN(name)
name = "Bob" l = len(name)
name = "Bob" l = name.length
{ not specified in eduqas pseudocode reference }
Getting a character from a string
   

A string is some text such as "Hello"

A character is a single letter or digit.

Sometimes it's useful to get the 1st, 2nd or whichever character from a string.

This example gets the first character of a string called name and stores it into a variable called first_letter

STRING name SET name TO "Bob" CHAR first_letter SET first_letter TO name[0]
Dim name As String name = "Bob" Dim first_letter As Char first_letter = name(0)
name = "Bob" first_letter = name[0]
name = "Bob" first_letter = name.subString(0,1)
{ not specified in eduqas pseudocode reference }
Displaying a mixture of text and variables
    

Variables let your program store data of a particular data type.

Integer variables store whole numbers. String variables store text.

Sometimes you need to combine text with integer variables and string variables when you display it to the screen.

This example has a string variable called name (set to "Bob") and an integer variable called score (set to 10). It combines both variables with some text to display the message "Bob has 10 points"

INTEGER score SET score TO 10 STRING name SET name TO "Bob" SEND name & " has " & score & " points"
Dim score As Integer score = 10 Dim name As String name = "Bob" MsgBox(name & " has " & score & " points")
score = 10 name = "Bob" print(name + " has " + str(score) + " points")
score = 10 name = "Bob" print(name + " has " + str(score) + " points")
score is integer set score = 10 name is string set name = "Bob" output name + " has " + score + " points"
Casting (converting) to a string
     

Often it's useful to convert a number (integer or real data type) to text (string data type) so that it can be processed or sent as an output later in the program.

This example converts the integer number 1000 to a string containing "1000"

INTEGER int_number = 1000 ' There's not explicit casting. ' It's done implicitly by the data types of the variables STRING str_number = int_number
Dim IntNumber as Integer IntNumber = 1000 Dim StrNumber as String StrNumber = IntNumber.toString()
int_number = 1000 str_number = str(1000)
int_number = 1000 str_number = str(1000)
{ not specified in eduqas pseudocode reference }
Casting (converting) to an integer
     

Often it's useful to round a decimal number (real data type) to a whole number (integer data type).

It can also be useful to convert a string representation of a number (e.g. one typed in by the user) to an integer

This example converts the string "1000" to the integer 1000

STRING str_number = "1000" ' There's not explicit casting. ' It's done implicitly by the data types of the variables INTEGER int_number = str_number
Dim StrNumber as String StrNumber = "1000" Dim IntNumber as Integer IntNumber = Convert.toInt32(StrNumber)
str_number = "1000" int_number = int(1000)
str_number = "1000" int_number = int(1000)
{ not specified in eduqas pseudocode reference }
Casting (converting) to a real
     

Often it's useful to convert a string representation of a number (e.g. one typed in by the user) to a decimal number for use in calculations (real data type)

This example converts the string "1.1234" to the real value 1.1234

STRING str_number = "1.1234" ' There's not explicit casting. ' It's done implicitly by the data types of the variables REAL real_number = str_number
Dim StrNumber as String StrNumber = "1.1234" Dim DblNumber as Double DblNumber = Convert.toDouble(StrNumber)
str_number = "1.1234" float_number = float(str_number)
str_number = "1.1234" float_number = float(str_number)
{ not specified in eduqas pseudocode reference }

Loading...