C# String Access - Complete Guide
Introduction to String Access
Accessing individual characters and substrings is a common task in C#. Strings behave like character arrays, so you can use indexing and iteration. Additionally, methods like Substring and the range operator provide flexible ways to extract parts of a string.
Efficient string access is essential for parsing, text manipulation, and working with structured or formatted data.
Character Access and Indexing
You can access string characters using indexes (starting at 0). Strings are immutable, so modifying characters requires creating a new string (e.g., via a char array).
using System;
namespace StringAccessExample
{
class Program
{
static void Main(string[] args)
{
string text = "Hello, World!";
Console.WriteLine($"Original string: {text}");
Console.WriteLine($"First character: {text[0]}");
Console.WriteLine($"Seventh character: {text[6]}");
Console.WriteLine($"Last character: {text[text.Length - 1]}");
Console.WriteLine("\nAll characters with ASCII values:");
for (int i = 0; i < text.Length; i++)
{
Console.WriteLine($"Index {i}: '{text[i]}' ({(int)text[i]})");
}
Console.WriteLine("\nUsing foreach:");
foreach (char c in text)
{
Console.Write(c + " ");
}
Console.WriteLine();
char[] chars = text.ToCharArray();
chars[0] = 'J';
chars[7] = 'w';
string modified = new string(chars);
Console.WriteLine($"Modified string: {modified}");
}
}
}
Original string: Hello, World! First character: H Seventh character: Last character: ! All characters with ASCII values: Index 0: 'H' (72) Index 1: 'e' (101) Index 2: 'l' (108) Index 3: 'l' (108) Index 4: 'o' (111) Index 5: ',' (44) Index 6: ' ' (32) Index 7: 'W' (87) Index 8: 'o' (111) Index 9: 'r' (114) Index 10: 'l' (108) Index 11: 'd' (100) Index 12: '!' (33) Using foreach: H e l l o , W o r l d ! Modified string: Jello, world!
Substring and Range Operations
C# provides Substring() and range operators (C# 8.0+) to extract sections of strings.
You can also split strings into arrays of words or use regular expressions for more advanced text extraction.
using System;
using System.Text.RegularExpressions;
namespace SubstringExample
{
class Program
{
static void Main(string[] args)
{
string text = "The quick brown fox jumps over the lazy dog";
Console.WriteLine($"Original: {text}");
Console.WriteLine($"Substring from index 4: '{text.Substring(4)}'");
Console.WriteLine($"Substring from index 4, length 5: '{text.Substring(4, 5)}'");
Console.WriteLine($"First 10 characters: '{text[0..10]}'");
Console.WriteLine($"Last 10 characters: '{text[^10..]}'");
Console.WriteLine($"Characters 10–20: '{text[10..20]}'");
int start = text.IndexOf("brown");
int end = text.IndexOf(" ", start);
if (end == -1) end = text.Length;
string word = text.Substring(start, end - start);
Console.WriteLine($"\nFound word: '{word}'");
string[] words = text.Split(' ');
Console.WriteLine("\nWords:");
foreach (string w in words)
{
Console.WriteLine("- " + w);
}
Regex regex = new Regex(@"\b\w{5}\b");
var matches = regex.Matches(text);
Console.WriteLine("\n5-letter words:");
foreach (Match match in matches)
{
Console.WriteLine($"- '{match.Value}' at index {match.Index}");
}
}
}
}
Original: The quick brown fox jumps over the lazy dog Substring from index 4: 'quick brown fox jumps over the lazy dog' Substring from index 4, length 5: 'quick' First 10 characters: 'The quick ' Last 10 characters: ' lazy dog' Characters 10–20: 'brown fox ' Found word: 'brown' Words: - The - quick - brown - fox - jumps - over - the - lazy - dog 5-letter words: - 'quick' at index 4 - 'brown' at index 10 - 'jumps' at index 20