Knowledge Organiser: String Handling
Key Terms
- String: A sequence of characters stored as text
- Concatenation: Joining two strings together using + (e.g. "Hello" + " " + "World")
- Substring: Extracting part of a string using string.substring(start, length)
- Length: The number of characters in a string, accessed with string.length
- ASC: Converts a character to its ASCII code number (e.g. ASC("A") = 65)
- CHR: Converts an ASCII code number to a character (e.g. CHR(65) = "A")
Must-Know Facts
- Strings are 0-indexed: the first character is at position 0
- substring(start, length) takes a starting position and a number of characters, NOT an end position
- String operations return a new string — the original is not changed
- .upper and .lower convert case for comparison purposes
- ASC("A") = 65, ASC("a") = 97 — upper and lower case have different codes
Key Concepts
- Get length: "Hello".length returns 5
- Extract chars: "Hello".substring(0, 2) returns "He"
- Convert case: "Hello".upper returns "HELLO"
- Join strings: "First" + " " + "Last" returns "First Last"
- ASCII: ASC("A") = 65; CHR(65) = "A"
Common Mistakes
- Using 1-based indexing instead of 0-based: Strings are 0-indexed in most pseudocode — the first character is at position 0, not 1
- Confusing substring's second argument: substring(start, length) takes a LENGTH (number of characters), not an end position — substring(2, 3) gets 3 characters from position 2
- Forgetting case when comparing strings: "Hello" does not equal "hello" — use .upper or .lower to standardise case before comparison
- Confusing ASC and CHR: ASC converts a character TO its ASCII number; CHR converts an ASCII number TO a character — they work in opposite directions
- Trying to add a number directly to a string: Concatenation joins strings — a number must be converted to a string first before joining with +