String handling
There are several built-in stringA sequence of characters often stored as a variable in a computer program. These characters can include numbers, letters and symbols. functions in most programming languageA language used by a programmer to write a piece of software. . They do things like calculate the length of a string or extract a part of a string. Since they are so common, they are also included in pseudocode Also written as pseudo-code. A method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding. and the functions in this section use the Eduqas pseudocode versions.
String passing
As in other functions, a string function will typically expect a string value as an argument to the function, then return the result to the calling program.
User-defined functions can also be written to process strings, passing string values for purposes not covered by the built-in functions.
Concatenation
Concatenation means joining two strings together. In the following program, three variableA memory location within a computer program where values are stored. are defined as strings and the firstname and surname values are set. The final variable, fullname, is created by joining the firstname and surname values together with a space between the strings:
firstname is string surname is string fullname is string set firstname = “Bob” set surname = “Smith” set fullname = firstname & " " & surnameFinding the length of a string
To find the length of a string the function len is used.
word is string set word = “Computer Science” output len(word)The output in this example would be 16 because it includes the space between the words.
String comparison
There are two main ways of comparing strings of dataUnits of information. In computing there can be different data types, including integers, characters and Boolean. Data is often acted on by instructions.:
- checking if there is an exact content match
- comparing if there is an exact content match but ignoring differences in upper and lower case letters
The way to do this is using the strComp function:
strComp(string1, string2)Checking if there is an exact match
In this example, two words are compared. If they exactly match then the program displays “They match”. Otherwise it displays the message “No match”:
word1 is string word2 is string set word1 = “Computer Science” set word2 = “Computing” if strComp(word1,word2) output “They match” else output “No match” end ifIgnoring differences in case
To compare if there is an exact content match, ignoring differences in upper and lower case letters requires the use of another parameter, namely adding a ‘1’:
Name1 is string Name2 is string Name1 = “Geri” Name2 = “geri” If strComp(Name1,Name2,1) output “The content matches” else output “The content doesn’t match” end ifSelecting all or part of a string
The mid function is used to find all or part of a string:
Mid(string,startpoint,endpoint) word is string part is string set word = “Computer Science” part = mid(word,10,16) output partThe output would be “Science”.
Trimming a string
Trimming a string allows additional spaces at the start or end of a string to be removed:
word is string set word = “ Computer ” newWord = trim(word)Replacing part of a string
To replace part of a string the replace function is used:
Replace(string, find, replacement) UnhealthyPhrase is string HealthyPhrase is string Set UnhealthyPhrase = “How many sweets do you want?” HealthyPhrase = replace(UnhealthyPhrase, “sweets”, “pieces of fruit”) Output HealthyPhraseThe output would be “How many pieces of fruit do you want?”
More guides on this topic
- The CPU - Eduqas
- Primary storage - Eduqas
- Secondary storage and embedded systems - Eduqas
- Networks - Eduqas
- Internet and cybersecurity - Eduqas
- Data representation - Eduqas
- Storage and data organisation - Eduqas
- Operating systems - Eduqas
- Principles of programming - Eduqas
- Sorting, searching and validation - Eduqas
- Software development - Eduqas
- Impacts of digital technology on wider society - Eduqas