String handling operations

String Handling Operations

A string is a sequence of characters. Programming languages provide a set of built-in operations for inspecting and manipulating strings: finding their length, locating characters, extracting sections, joining strings together, converting between characters and their numeric codes, and converting strings to numbers and back. Together these operations allow programs to process text in almost any way needed.

OperationAQA pseudocodePythonVB.NETC#
LengthLEN(str)len(str)str.Lengthstr.Length
PositionPOSITION(str, ch)str.index(ch)str.IndexOf(ch)str.IndexOf(ch)
SubstringSUBSTRING(str, start, len)str[start:start+len]str.Substring(start, len)str.Substring(start, len)
Concatenationstr1 + str2str1 + str2str1 & str2str1 + str2
Char to codeASC(ch)ord(ch)Asc(ch)(int)str[0]
Code to charCHR(n)chr(n)Chr(n)(char)n
String to integer-int(str)CInt(str)int.Parse(str)
String to real-float(str)CDbl(str)double.Parse(str)
Integer to string-str(n)CStr(n)n.ToString()
Real to string-str(f)CStr(f)f.ToString()

All index values are 0-based: the first character is at position 0.

Operations in Practice

LEN returns the total number of characters in a string. POSITION returns the index of the first occurrence of a character or substring (0 for the first position). SUBSTRING extracts a section starting at a given index and running for a given number of characters.

word ← "Python"
OUTPUT LEN(word)                   ⟶ 6
OUTPUT POSITION(word, "t")         ⟶ 2
OUTPUT SUBSTRING(word, 0, 3)       ⟶ "Pyt"
OUTPUT SUBSTRING(word, 2, 4)       ⟶ "thon"
# Python -- length, position, substring
word = "Python"

print(len(word))                    # 6
print(word.index("t"))              # 2  (0-based: P=0, y=1, t=2)
print(word[0:3])                    # "Pyt"   -- slice [start : start+length]
print(word[2:6])                    # "thon"  -- from index 2, 4 characters
print(word[2:2+4])                  # same as above, written explicitly
' VB.NET -- length, position, substring
Dim word As String = "Python"

Console.WriteLine(word.Length)              ' 6
Console.WriteLine(word.IndexOf("t"))        ' 2  (0-based)
Console.WriteLine(word.Substring(0, 3))     ' "Pyt"
Console.WriteLine(word.Substring(2, 4))     ' "thon"
// C# -- length, position, substring
string word = "Python";

Console.WriteLine(word.Length);              // 6
Console.WriteLine(word.IndexOf("t"));        // 2  (0-based)
Console.WriteLine(word.Substring(0, 3));     // "Pyt"
Console.WriteLine(word.Substring(2, 4));     // "thon"

Concatenation joins two or more strings end-to-end to produce a new string. In AQA pseudocode and Python, the + operator is used. VB.NET uses the & operator to avoid ambiguity with numeric addition. C# also uses +.

first ← "Alice"
last  ← "Smith"
full  ← first + " " + last
OUTPUT full       ⟶ "Alice Smith"
# Python -- concatenation with +
first = "Alice"
last  = "Smith"
full  = first + " " + last     # join with a space in between
print(full)                     # Alice Smith

greeting = "Hello, " + first + "!"
print(greeting)                 # Hello, Alice!
' VB.NET -- concatenation with & (preferred over + for strings)
Dim first As String = "Alice"
Dim last  As String = "Smith"
Dim full  As String = first & " " & last    ' join with a space
Console.WriteLine(full)                      ' Alice Smith

Dim greeting As String = "Hello, " & first & "!"
Console.WriteLine(greeting)                  ' Hello, Alice!
// C# -- concatenation with +
string first = "Alice";
string last  = "Smith";
string full  = first + " " + last;    // join with a space
Console.WriteLine(full);               // Alice Smith

string greeting = "Hello, " + first + "!";
Console.WriteLine(greeting);           // Hello, Alice!

Every character is stored as a number internally - its character code (based on ASCII/Unicode). ASC / ord() converts a character to its code; CHR / chr() converts a code back to a character. Upper-case letters A-Z have codes 65-90; lower-case a-z have codes 97-122.

OUTPUT ASC("A")    ⟶ 65
OUTPUT ASC("a")    ⟶ 97
OUTPUT CHR(66)     ⟶ "B"
OUTPUT CHR(98)     ⟶ "b"
# Python -- ord() and chr()
print(ord("A"))    # 65
print(ord("a"))    # 97
print(chr(66))     # B
print(chr(98))     # b

# Practical: shift a letter one place up the alphabet
letter = "C"
shifted = chr(ord(letter) + 1)
print(shifted)     # D
' VB.NET -- Asc() and Chr()
Console.WriteLine(Asc("A"))    ' 65
Console.WriteLine(Asc("a"))    ' 97
Console.WriteLine(Chr(66))     ' B
Console.WriteLine(Chr(98))     ' b

' Practical: shift a letter one place up the alphabet
Dim letter As String = "C"
Dim shifted As String = Chr(Asc(letter) + 1)
Console.WriteLine(shifted)     ' D
// C# -- cast between char and int
string s = "A";
Console.WriteLine((int)s[0]);       // 65  (cast first char to int)
Console.WriteLine((int)"a"[0]);     // 97
Console.WriteLine((char)66);        // B   (cast int to char)
Console.WriteLine((char)98);        // b

// Practical: shift a letter one place up the alphabet
string letter  = "C";
string shifted = ((char)((int)letter[0] + 1)).ToString();
Console.WriteLine(shifted);         // D

Strings that look like numbers can be converted to numeric types so that arithmetic can be performed, and numbers can be converted to strings for output or concatenation. AQA specifies four conversion directions: string to integer, string to real, integer to string, and real to string.

# Python -- string conversion operations
# String to Integer
n = int("42")
print(n + 1)           # 43

# String to Real
f = float("3.14")
print(f * 2)           # 6.28

# Integer to String
s = str(100)
print("Score: " + s)   # Score: 100

# Real to String
t = str(9.81)
print("Value: " + t)   # Value: 9.81
' VB.NET -- string conversion operations
' String to Integer
Dim n As Integer = CInt("42")
Console.WriteLine(n + 1)                ' 43

' String to Real
Dim f As Double = CDbl("3.14")
Console.WriteLine(f * 2)                ' 6.28

' Integer to String
Dim s As String = CStr(100)
Console.WriteLine("Score: " & s)        ' Score: 100

' Real to String
Dim t As String = CStr(9.81)
Console.WriteLine("Value: " & t)        ' Value: 9.81
// C# -- string conversion operations
// String to Integer
int n = int.Parse("42");
Console.WriteLine(n + 1);              // 43

// String to Real
double f = double.Parse("3.14");
Console.WriteLine(f * 2);              // 6.28

// Integer to String
string s = (100).ToString();
Console.WriteLine("Score: " + s);      // Score: 100

// Real to String
string t = (9.81).ToString();
Console.WriteLine("Value: " + t);      // Value: 9.81

 Key Takeaways

  • All string indices are 0-based: the first character is at position 0, not 1.
  • LEN / len() / .Length returns the number of characters. POSITION / .index() / .IndexOf() returns the index of the first match.
  • SUBSTRING(str, start, length) in AQA pseudocode maps to str[start:start+length] in Python and .Substring(start, length) in VB.NET and C#.
  • VB.NET uses & for concatenation; Python and C# use +. Mixing + with non-strings in VB.NET causes errors.
  • ASC / ord() converts a character to its numeric code; CHR / chr() / (char) converts a code back to a character.
  • To include a number inside a concatenated string it must first be converted to a string using str(), CStr(), or .ToString().