Overview
A string is an ordered sequence of characters or encoded units representing text. Depending on the language, strings may be implemented as immutable objects, mutable character arrays, or specialized sequences with encoding and length metadata.
Purpose
Strings provide a standard way to represent textual information and support operations such as searching, comparing, joining, extracting, and transforming text.
Key concepts
name: Core idea; description: The central focus of strings as data structures is the capability described here: A string is a sequence of text units used to represent and process textual information such as words, sentences, identifiers, and messages.; name: Problem context; description: Strings as Data Structures is relevant when a computational task requires the behavior or capability described by this topic.; name: Correct use; description: Before using strings as data structures, check its assumptions, input requirements, expected output, and resource constraints.
Complexity
operation: Length retrieval; typicalComplexity: O(1) when length metadata is stored; may require traversal in some representations.; operation: Character access; typicalComplexity: Often O(1) for indexed representations, but the unit accessed may be a byte or code unit rather than a user-perceived character.; operation: Concatenation; typicalComplexity: Often O(n + m) when creating a new combined string of lengths n and m.; operation: Substring search; typicalComplexity: Depends on the search algorithm; straightforward approaches can take O(nm), while specialized algorithms can achieve linear-time bounds.; operation: Comparison; typicalComplexity: O(k) in the worst case for comparing strings up to the first differing position, where k is bounded by their lengths.
Worked example
scenario: Processing the string 'SCIENEXY'.; string: SCIENEXY; length: 8; firstCharacter: S; concatenation: "SCI" + "ENCE" + "XY"; result: SCIENCEXY
Applications
Usernames and passwords.; Messages and documents.; Source code and programming-language tokens.; Search queries.; File paths and URLs.; Text processing and natural language applications.
Advantages
Provides a convenient representation for text.; Supports searching and transformation operations.; Can be integrated with input/output and file processing.; Immutable strings can simplify reasoning about shared text values.