Data Structure
String
An ordered sequence of characters — conceptually an array of characters with rich text-specific operations.
Text as a sequence.
A string is a sequence of characters like "hello". You can read
individual characters by index, get its length, slice out substrings, and join strings together
(concatenation).
- Indexing —
s[0]is the first character. - Concatenation —
"foo" + "bar"→"foobar". - Common ops — length, substring, search, split, replace, case conversion.
Immutability and building.
In many languages (Java, Python, C#) strings are immutable — every "modification"
creates a new string. Repeated concatenation in a loop is therefore O(n²); use a mutable builder
(StringBuilder, list + join) for O(n).
encoding matters
ASCII 1 byte per char UTF-8 1–4 bytes per char (variable) UTF-16 2 or 4 bytes (surrogate pairs)
"Length" can mean bytes, code units, or code points — a subtlety that bites with emoji and non-Latin scripts.
Interning, matching, and internals.
- String interning — pooling identical string literals so equal strings share one object, saving memory and enabling reference equality.
- Pattern matching — naive search is O(n·m); KMP, Rabin–Karp (rolling hash), and Boyer–Moore achieve better bounds.
- Ropes & gap buffers — specialized structures for very large or heavily-edited text (editors).
- Unicode correctness — normalization forms, grapheme clusters, and locale-aware comparison matter for real-world text handling.