Understanding Strings
What Is a String?
A string is a sequence of characters used to store text. It can include letters, numbers, symbols, and even spaces. Strings are one of the most frequently used data types in programming because almost every application needs to display or process text.
In many programming languages, including C#, strings are treated as special objects with built-in features that make them easy to use.
How Strings Are Stored in Memory
In most languages, including C#, strings are immutable. This means once a string is created, it cannot be changed. When you modify a string, a new string is created in memory.
Example:
string name = "John";
name = name + " Doe";
Behind the scenes, this creates a new string "John Doe" instead of modifying the original.
Why Strings Are Immutable
For security
For performance optimization
To support string interning
Because strings are used very often, immutability helps the system manage memory efficiently.
Common String Operations
Strings support many useful operations:
1. Accessing characters
char c = name[0];
2. Finding length
int length = name.Length;
3. Substring extraction
string part = name.Substring(1, 3);
4. Searching inside a string
bool result = name.Contains("John");
5. Splitting a string
string[] items = sentence.Split(' ');
6. Converting to uppercase or lowercase
string upper = word.ToUpper();
Time Complexity of String Operations
Access character by index: O(1)
Concatenation: O(n) (because a new string is created)
Searching: O(n)
Splitting: O(n)
To handle repeated concatenation efficiently, you should use StringBuilder.
Example:
StringBuilder sb = new StringBuilder();
sb.Append("Hello");
sb.Append(" World");
string final = sb.ToString();
Real-Life Uses of Strings
Strings are used everywhere:
Usernames and passwords
Search bars
Chat messages
File paths
URLs
Data entry forms
Any feature that deals with text uses strings internally.
String Comparison
Comparing two strings is a common operation.
if (name1 == name2)
{
Console.WriteLine("Same");
}
Case-insensitive comparison
if (string.Equals(s1, s2, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Match");
}
Pattern Searching in Strings
String searching is a key concept used in:
Text editors
Search engines
Validation systems
Two common approaches:
Naive search ? O(n*m)
Efficient algorithms (covered later)
KMP
Rabin-Karp
Example: Count Vowels in a String
string text = "Hello World";
int count = 0;
string vowels = "aeiouAEIOU";
for (int i = 0; i < text.Length; i++)
{
if (vowels.Contains(text[i]))
{
count++;
}
}
Console.WriteLine("Vowel Count: " + count);
Working with StringBuilder
When you need to modify a string multiple times, StringBuilder is the preferred choice.
Why?
It does not create new copies of strings
It is faster for repeated updates
Example:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++)
{
sb.Append(i + " ");
}
Console.WriteLine(sb.ToString());
Summary
Strings are an essential data structure used in every application. Understanding how they work, how to manipulate them, and how to optimize string operations is crucial for becoming a strong programmer.
Key takeaways:
Strings are immutable
Use StringBuilder for modifications
Many built-in methods make string handling easy
String operations often take O(n) time