Class SpanExtensions
Provides helper methods for trimming and slicing ReadOnlySpan<T> instances.
public static class SpanExtensions
- Inheritance
-
SpanExtensions
- Inherited Members
Methods
Mid<T>(ReadOnlySpan<T>, int)
Retrieves a sub-ReadOnlySpan<T> from this instance. The sub-ReadOnlySpan<T> starts at a specified character position.
public static ReadOnlySpan<T> Mid<T>(this ReadOnlySpan<T> s, int start)
Parameters
sReadOnlySpan<T>The string from which to extract the sub-string
startintThe zero-based character position at which the sub-string starts
Returns
- ReadOnlySpan<T>
A ReadOnlySpan<T> equivalent to the sub-ReadOnlySpan<T> starting from the index
start
Type Parameters
T
Examples
// Extract a sub-span starting from index 7 to the end.
ReadOnlySpan<char> originalSpan = "Hello, World!".AsSpan();
ReadOnlySpan<char> subSpan = originalSpan.Mid(7);
// subSpan contains "World!"
// Extract a sub-span starting from the end.
ReadOnlySpan<char> endSpan = originalSpan.Mid(-6);
// endSpan contains "World!"
Mid<T>(ReadOnlySpan<T>, int, int)
Retrieves a sub-ReadOnlySpan<T> from this instance. The sub-ReadOnlySpan<T> starts at a specified character position and has a defined length.
public static ReadOnlySpan<T> Mid<T>(this ReadOnlySpan<T> s, int start, int length)
Parameters
sReadOnlySpan<T>The string from which to extract the sub-string
startintThe zero-based character position at which the sub-string starts
lengthintThe number of characters in the sub-string
Returns
- ReadOnlySpan<T>
A ReadOnlySpan<T> equivalent to the sub-ReadOnlySpan<T> of length
lengththat begins atstartin this instance, or ReadOnlySpan<T>.Empty ifstartis equal to the length of this instance andlengthis zero.
Type Parameters
T
Examples
// Extract a sub-span of length 5 starting from index 2.
ReadOnlySpan<char> originalSpan = "Hello, World!".AsSpan();
ReadOnlySpan<char> subSpan = originalSpan.Mid(2, 5);
// subSpan contains "llo, "
// Extract a sub-span of length 5 starting from the end.
ReadOnlySpan<char> endSpan = originalSpan.Mid(-5, 5);
// endSpan contains "World"
TrimEnd<T>(ReadOnlySpan<T>, Func<T, bool>)
Removes elements from the end of s that match the result of the specified function.
public static ReadOnlySpan<T> TrimEnd<T>(this ReadOnlySpan<T> s, Func<T, bool> trimTester)
Parameters
sReadOnlySpan<T>Reference span
trimTesterFunc<T, bool>Test function (returns true to remove the element)
Returns
- ReadOnlySpan<T>
Span with removed elements
Type Parameters
T
TrimStart<T>(ReadOnlySpan<T>, Func<T, bool>)
Removes elements from the beginning of s that match the result of the specified function.
public static ReadOnlySpan<T> TrimStart<T>(this ReadOnlySpan<T> s, Func<T, bool> trimTester)
Parameters
sReadOnlySpan<T>Reference span
trimTesterFunc<T, bool>Test function (returns true to remove the element)
Returns
- ReadOnlySpan<T>
Span with removed elements
Type Parameters
T
Trim<T>(ReadOnlySpan<T>, Func<T, bool>)
Removes elements from the beginning and end of s that match the result of the specified function.
public static ReadOnlySpan<T> Trim<T>(this ReadOnlySpan<T> s, Func<T, bool> trimTester)
Parameters
sReadOnlySpan<T>Reference span
trimTesterFunc<T, bool>Test function (returns true to remove the element)
Returns
- ReadOnlySpan<T>
Span with removed elements
Type Parameters
T