String Handling (CIE IGCSE Computer Science)

Revision Note

Test Yourself
Becci Peters

Expertise

Computer Science

String Handling

  • Strings are a sequence of characters, such as letters, numbers, and symbols
  • They are used to represent text in a computer program
  • String handling refers to the various operations that can be performed on strings

Length

  • The length of a string is the number of characters it contains
  • In Python, the len() function is used to find the length of a string
  • In Java, the length() method is used to find the length of a string
  • In Visual Basic, the Len() function is used to find the length of a string

Substring

  • A substring is a portion of a string
  • In Python, the substring can be obtained using the slicing operator [:]
  • In Java, the substring() method is used to obtain a substring
  • In Visual Basic, the Mid() function is used to obtain a substring

Upper

  • The upper() method converts all characters of a string to uppercase
  • In Python, the upper() method is used to convert a string to uppercase
  • In Java, the toUpperCase() method is used to convert a string to uppercase
  • In Visual Basic, the UCase() function is used to convert a string to uppercase

Lower

  • The lower() method converts all characters of a string to lowercase
  • In Python, the lower() method is used to convert a string to lowercase
  • In Java, the toLowerCase() method is used to convert a string to lowercase
  • In Visual Basic, the LCase() function is used to convert a string to lowercase

Pseudocode example:

string phrase "Save my exams"

length LENGTH(phrase)

substring SUBSTRING(phrase, 5, 2)

upper TO_UPPER(phrase)

lower TO_LOWER(phrase)

Python example:

phrase = "Save my exams"

length = len(phrase)

substring = phrase[4:6]

upper = phrase.upper()

lower = phrase.lower()

Java example:

String phrase = "Save my exams";

int length = phrase.length();

String substring = phrase.substring(4, 6);

String upper = phrase.toUpperCase();

String lower = phrase.toLowerCase();

Visual basic example:

Dim phrase As String = "Save my exams"

Dim length As Integer = phrase.Length

Dim substring As String = phrase.Substring(4, 2)

Dim upper As String = phrase.ToUpper()

Dim lower As String = phrase.ToLower()

Position of First Character

  • The first character of a string can be at position zero or one, depending on the programming language
  • In Python and Java, the first character of a string is at position zero
  • In Visual Basic, the first character of a string is at position one

Worked example

The function Length(x) finds the length of a string x. The function substring(x,y,z) finds a substring of x starting at position y and z characters long. The first character in x is in position 1.

Write pseudocode statements to:

  • Store the string “Save my exams” in x
  • Find the length of the string and output it
  • Extract the word exams from the string and output it

[6]

  X "Save my exams" [1] for storing string in X.
  OUTPUT Length(X) [1] for calling the function length. [1] for using the correct parameter X.
 

Y 9

Z

 
  OUTPUT SubString(X,Y,Z)

 [1] for using the substring function. [1] for correct parameters.

[1] for outputting length and substring return values.

 

Exam Tip

  • Remember that the length of a string is not the same as the index of the last character in the string
  • Be careful when counting positions in a string, as the first character can be at position zero or one depending on the programming language

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Becci Peters

Author: Becci Peters

Becci has been a passionate Computing teacher for over 9 years, teaching Computing across the UK helping to engage, interest and develop confidence in the subject at all levels. Working as a Head of Department and then as an educational consultant, Becci has advised schools in England, where her role was to support and coach teachers to improve Computing teaching for all. Becci is also a senior examiner for multiple exam boards covering GCSE & A-level. She has worked as a lecturer at a university, lecturing trainee teachers for Computing.