diff --git a/docs/standard/base-types/divide-up-strings.md b/docs/standard/base-types/divide-up-strings.md index e85ba5aa25ad4..8638aadeb3b44 100644 --- a/docs/standard/base-types/divide-up-strings.md +++ b/docs/standard/base-types/divide-up-strings.md @@ -1,12 +1,13 @@ --- title: Separate strings into substrings -description: Learn about different techniques to extract parts of a string, including String.Split, regular expressions, and String.Substring. -ms.date: 10/30/2020 +description: Learn about different techniques to extract parts of a string, including String.Split, regular expressions, String.Substring, and the range operator. +ms.date: 03/20/2026 dev_langs: - "csharp" - "vb" helpviewer_keywords: - "strings [.NET], breaking up" +ai-usage: ai-assisted --- # Extract substrings from a string @@ -15,6 +16,7 @@ This article covers some different techniques for extracting parts of a string. - Use the [Split method](#stringsplit-method) when the substrings you want are separated by a known delimiting character (or characters). - [Regular expressions](#regular-expressions) are useful when the string conforms to a fixed pattern. - Use the [IndexOf and Substring methods](#stringindexof-and-stringsubstring-methods) in conjunction when you don't want to extract *all* of the substrings in a string. +- Use [ranges and indices](#ranges-and-indices) in C# to extract or trim characters at known positions. ## String.Split method @@ -112,7 +114,23 @@ The following example uses the method to find th :::code language="csharp" source="snippets/parse-strings/csharp/indexof.cs" id="1"::: :::code language="vb" source="snippets/parse-strings/vb/indexof.vb" id="1"::: +## Ranges and indices + +The C# [range operator `..`](../../csharp/language-reference/operators/member-access-operators.md#range-operator-) and the [index-from-end operator `^`](../../csharp/language-reference/operators/member-access-operators.md#index-from-end-operator-) let you extract substrings using a concise syntax. You can apply these operators directly to strings without calling . + +The following example shows several ways to extract parts of a string using ranges: + +:::code language="csharp" source="snippets/parse-strings/csharp/range.cs" id="snippet1"::: + +The next example uses the index-from-end operator to remove a file extension (the last three characters) from a path: + +:::code language="csharp" source="snippets/parse-strings/csharp/range.cs" id="snippet2"::: + +> [!NOTE] +> Ranges and the index-from-end operator are C# features. Visual Basic doesn't support this syntax; use instead. + ## See also - [.NET regular expressions](regular-expressions.md) - [How to parse strings using String.Split in C#](../../csharp/how-to/parse-strings-using-split.md) +- [Indices and ranges (C# guide)](../../csharp/tutorials/ranges-indexes.md) diff --git a/docs/standard/base-types/snippets/parse-strings/csharp/range.cs b/docs/standard/base-types/snippets/parse-strings/csharp/range.cs new file mode 100644 index 0000000000000..47ffb1d81bb37 --- /dev/null +++ b/docs/standard/base-types/snippets/parse-strings/csharp/range.cs @@ -0,0 +1,36 @@ +public class RangeExamples +{ + public static void Example1() + { + // + string str = "Hello, World!"; + + // Get the first 5 characters. + string hello = str[..5]; + Console.WriteLine(hello); + // Output: Hello + + // Get the last 6 characters. + string world = str[^6..]; + Console.WriteLine(world); + // Output: World! + + // Get characters from index 7 through 11 (exclusive of 12). + string substr = str[7..12]; + Console.WriteLine(substr); + // Output: World + // + } + + public static void Example2() + { + // + string filePath = "C:\\Users\\user1\\bin\\fileA.cs"; + + // Remove the last 3 characters (.cs extension). + string trimmedPath = filePath[..^3]; + Console.WriteLine(trimmedPath); + // Output: C:\Users\user1\bin\fileA + // + } +}