Ranges with Arrays and Strings
Slice arrays, spans, and substrings.
Ranges with Arrays and Strings is a free C# Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Slicing Arrays
The most common use of ranges is slicing arrays. array[1..3] returns a new array containing the selected elements.
using System;
int[] data = { 10, 20, 30, 40, 50 };
int[] middle = data[1..3];
Console.WriteLine(string.Join(",", middle)); // 20,30Array Slices Are Copies
For arrays, a range slice allocates a new array and copies the elements. Mutating the slice does not affect the original.
using System;
int[] original = { 1, 2, 3, 4 };
int[] slice = original[0..2];
slice[0] = 99;
Console.WriteLine(original[0]); // 1 (unchanged)
Console.WriteLine(slice[0]); // 99Substrings via Range
The string indexer supports Range, giving you substrings without calling Substring with offsets and lengths.
using System;
string text = "Hello, World";
string hello = text[0..5];
Console.WriteLine(hello); // HelloFrom-End String Slices
Combine ranges with ^ to grab the tail of a string, such as a file extension.
using System;
string file = "report.csv";
string ext = file[^3..];
Console.WriteLine(ext); // csvSingle Character with Index
Indexing a string with a single Index returns one char. Use ^1 for the final character.
using System;
string s = "Coddy";
char last = s[^1];
Console.WriteLine(last); // yDropping the First or Last
Open-ended ranges make trimming one element trivial: ..^1 drops the last, 1.. drops the first.
using System;
string quoted = "[value]";
string inner = quoted[1..^1];
Console.WriteLine(inner); // valueSplitting a String into Parts
You can split text by a known position using two range slices.
using System;
string code = "2026-05-30";
string year = code[..4];
string month = code[5..7];
string day = code[8..];
Console.WriteLine(year + " / " + month + " / " + day); // 2026 / 05 / 30Spans for Zero-Copy Slicing
If you want a slice without allocating, use AsSpan(). A span slice is a window over the original memory, not a copy.
using System;
ReadOnlySpan<char> span = "Hello, World".AsSpan();
ReadOnlySpan<char> word = span[7..];
Console.WriteLine(word.ToString()); // WorldRanges in Loops
You can iterate over a sliced array directly with foreach.
using System;
int[] data = { 2, 4, 6, 8, 10 };
foreach (int n in data[1..4])
Console.Write(n + " "); // 4 6 8
Console.WriteLine();Bounds Are Checked
Out-of-range slices throw ArgumentOutOfRangeException, so accidental over-reads fail fast instead of corrupting data.
using System;
int[] d = { 1, 2, 3 };
try { var _ = d[1..10]; }
catch (ArgumentOutOfRangeException) { Console.WriteLine("slice out of bounds"); }A Practical Parser
Here is a tiny parser that extracts a name and domain from an email using ranges and IndexOf.
using System;
string email = "ada@coddy.dev";
int at = email.IndexOf('@');
string user = email[..at];
string domain = email[(at + 1)..];
Console.WriteLine(user + " | " + domain); // ada | coddy.devQuick Check
Check your grasp of range slicing on arrays and strings.
Recap
You learned how ranges apply to arrays and strings.
array[1..3]returns a new copied array.string[a..b]returns a substring; a singleIndexreturns achar...^1and1..trim one element.AsSpan()slices without allocating.
Frequently asked questions
Is the “Ranges with Arrays and Strings” lesson free?
Yes — the full text of “Ranges with Arrays and Strings” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Ranges with Arrays and Strings”?
Slice arrays, spans, and substrings. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Ranges with Arrays and Strings” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Index Type and ^ Operator
- The Range Type and .. Operator
- Ranges with Arrays and Strings
- Custom Types Supporting Ranges