Home
Map
String SliceDevelop a Slice extension method on the string type. Slice gets a substring between 2 indexes.
C#
This page was last reviewed on Oct 25, 2022.
String slice. A string can be sliced. To do this, we provide an extension method. By slicing a string, we extract the characters from index one to index two.
Shows a string slice
Slice notes. Taking a slice is essentially the same as calling Substring with 2 index values. But sometimes using a second argument of the last index is easier.
String Substring
Array Slice
Input and output. The key thing to remember with slice is that the second argument is an end index, not a length. And we can use a negative end index to count from the end.Shows a string slice
Slice(1, 4) Peaceful eac Slice(3, -2) The morning is upon us. morning is upon u
Example. Here we develop a method in C# to match JavaScript. With the JavaScript slice method, you pass it the start index, which must always be 0 or higher.
And The second parameter can be any integer. When it is negative, it means you start from the end and then count backwards.
Note In JavaScript you can omit the second parameter. To duplicate this, create an overload that uses Length-1 as the second parameter.
Info The first character of the second test, which is similar to the Mozilla test, shows the first character as a space.
Detail My interpretation is that this is correct—the space is at index 3, while "m" is index 4.
using System; class Program { static void Main() { // // Tests from Mozilla // Console.WriteLine("Peaceful".Slice(1, 4)); // eac Console.WriteLine("The morning is upon us.".Slice(3, -2)); // " morning is upon u" string s = "0123456789_"; // // These slices are valid: // Console.WriteLine(s.Slice(0, 1)); // First char Console.WriteLine(s.Slice(0, 2)); // First two chars Console.WriteLine(s.Slice(1, 2)); // Second char Console.WriteLine(s.Slice(8, 11)); // Last three chars } } public static class Extensions { /// <summary> /// Get the string slice between the two indexes. /// Inclusive for start index, exclusive for end index. /// </summary> public static string Slice(this string source, int start, int end) { if (end < 0) // Keep this for negative end support { end = source.Length + end; } int len = end - start; // Calculate length return source.Substring(start, len); // Return Substring of length } }
eac morning is upon u 0 01 1 89_
Extension notes. This is an extension method. To create an extension method, use the "this" keyword as the first parameter in a static method (in a static class).
static
Extension
Notes, output. The required input and output is adapted from Mozilla. We can thus develop a Slice method equivalent to the JavaScript slice syntax.
A summary. Here we implemented a Slice method. An extension method makes the syntax clearer—slice is useful enough to justify an extension method.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.