String
Definition
A string is an ordered collection of characters enclosed in a pair of quotation marks, such as "Hello" or 'Programming', depending on the programming language.
More formally, a string is a sequence of zero or more characters treated as a single data item. Each character has a position in the sequence, and the positions are usually counted starting from zero.
Example:
"A"is a string of length 1"Unit 1"is a string containing letters, a space, and a digit""is an empty string, which contains no characters
Main Content
1. First Concept
String as a Sequence of Characters
- A string is not just a random group of letters; it is an ordered sequence of characters.
- The order matters greatly. For example,
"cat"and"tac"contain the same letters but are different strings because their character order is different.
A string may contain:
- Alphabets:
A,b,x - Numbers:
1,7,9 - Spaces:
"Hello World" - Symbols:
@,#,% - Mixed content:
"User123!"
Indexing and Position
- Each character in a string has a specific position called an index.
- In most languages, indexing begins at 0.
Example:
String: H e l l o
Index: 0 1 2 3 4
This means:
His at index 0eis at index 1ois at index 4
Length of a String
- The length of a string is the total number of characters it contains.
- Spaces and symbols are counted as characters too.
Examples:
"Hello"has length 5"Hello World"has length 11""has length 0
2. Second Concept
String Representation in Memory
- Computers store strings in memory as a sequence of character codes.
- Each character is represented using a numerical encoding such as ASCII or Unicode.
- This allows the computer to store, compare, and process text efficiently.
Example:
Amay be stored as a numeric codeBas another numeric code- Different systems may use different encodings, but Unicode is widely used today because it supports many languages and symbols
Mutability and Immutability
- In some languages, strings are immutable, meaning once created they cannot be changed directly.
- If a modification is needed, a new string is created.
- In other languages, strings may be mutable, meaning their contents can be changed.
Example of immutability:
- Original string:
"Hello" - If you change
HtoJ, the result is not the same object; a new string like"Jello"may be created
This is important because it affects:
- performance
- memory usage
- how string operations are implemented
String Literals
- A string written directly in code is called a string literal.
- It is usually enclosed in single quotes, double quotes, or backticks depending on the language.
Examples:
"Computer"
'Science'
"12345"
Some languages allow escape characters inside strings, such as:
\nfor newline\tfor tab\"for a double quote inside a string
Example:
"She said, \"Hello\""
3. Third Concept
Common String Operations
Strings are powerful because many operations can be performed on them.
Concatenation
- : Joining two or more strings
Example:
"Hello" + " " + "World"→"Hello World"
Comparison
- : Checking whether two strings are equal or which one comes first alphabetically
Example:
"apple"comes before"banana"
Substring extraction
- : Getting part of a string
Example: From
"Programming", the substring"gram"can be extracted
Searching
- : Finding whether a character or pattern exists in a string
Example: Checking if
"World"is inside"Hello World"
Replacement
- : Substituting one part of a string with another
Example:
"I like cats"→"I like dogs"
Splitting
- : Dividing a string into smaller parts
Example:
"red,green,blue"→["red", "green", "blue"]
Trimming
- : Removing spaces from the beginning or end
Example:
" hello "→"hello"
String Traversal
- Traversal means visiting each character one by one.
- This is useful when counting vowels, checking for palindromes, or analyzing text.
Example:
String: "DATA"
Characters: D → A → T → A
String as Input and Output
- Strings are frequently used for user input and output.
- When a user types a name or message, the program usually receives it as a string.
- Programs also display messages as strings.
Example:
- Input:
"Alice" - Output:
"Welcome, Alice!"
Working / Process
1. Creation of the String
- A string is created when text is written inside quotation marks or received from user input.
- Example:
"School"or a name typed into a text field.
2. Storage and Encoding
- The system stores each character using a numeric code such as ASCII or Unicode.
- The string is kept in memory as a sequence of encoded values.
3. Processing and Manipulation
- The program performs operations like searching, comparing, joining, or splitting.
- The string may be read character by character, modified, or used in calculations such as counting words or checking patterns.
Example process for reversing a string:
Original: "abcde"
Step 1: read last character first
Step 2: continue moving backward
Step 3: result becomes "edcba"
Example diagram for character positions:
String: P r o g r a m
Index: 0 1 2 3 4 5 6
This helps explain how each character is individually accessible.
Advantages / Applications
Text handling in software
- : Strings are used to store names, addresses, messages, documents, and labels in nearly all applications.
Easy communication between user and program
- : Input and output data are often exchanged as strings, making programs interactive and user-friendly.
Wide range of text processing tasks
- : Strings support important operations such as searching, formatting, sorting, validation, and data extraction.
Summary
- A string is a sequence of characters used to represent text.
- Characters in a string have order, position, and length.
- Strings are essential for storing and processing human-readable information.
- Important terms to remember: string, character, index, length, concatenation, substring, immutable