Struct StringSpan
Represents a span of characters within a string.
public readonly struct StringSpan : IEquatable<string>, IEquatable<StringSpan>
- Implements
- Inherited Members
Fields
Empty
Gets an empty StringSpan.
public static readonly StringSpan Empty
Field Value
Properties
this[int]
Gets the character at the specified index within the span.
public char this[int index] { get; }
Parameters
indexintThe index of the character.
Property Value
- char
The character at the specified index.
Length
Gets the length of the span.
public int Length { get; }
Property Value
String
Gets the underlying string.
public string String { get; }
Property Value
Methods
AsSpan()
Returns a read-only span of characters that represents the current span.
public ReadOnlySpan<char> AsSpan()
Returns
- ReadOnlySpan<char>
A read-only span of characters that represents the current span.
Equals(StringSpan)
Determines whether the current span is equal to the specified StringSpan.
public bool Equals(StringSpan other)
Parameters
otherStringSpanThe StringSpan to compare.
Returns
- bool
trueif the current span is equal to the specified StringSpan; otherwise,false.
Equals(object?)
public override bool Equals(object? obj)
Parameters
objobject
Returns
Equals(ReadOnlySpan<char>)
Determines whether the current span is equal to the specified read-only span of characters.
public bool Equals(ReadOnlySpan<char> other)
Parameters
otherReadOnlySpan<char>The read-only span of characters to compare.
Returns
- bool
trueif the current span is equal to the specified read-only span of characters; otherwise,false.
Equals(string?)
Determines whether the current span is equal to the specified string.
public bool Equals(string? other)
Parameters
otherstringThe string to compare.
Returns
- bool
trueif the current span is equal to the specified string; otherwise,false.
For(string)
Creates a StringSpan that represents the entire string.
public static StringSpan For(string @string)
Parameters
stringstringThe underlying string.
Returns
- StringSpan
A StringSpan that represents the entire string.
GetHashCode()
public override int GetHashCode()
Returns
GetOffsetAndLength()
Gets the offset of the span from the start of the source text and the length of the span.
public (int offset, int length) GetOffsetAndLength()
Returns
Skip(int)
Creates a new StringSpan that skips the specified number of characters from the start.
public StringSpan Skip(int count)
Parameters
countintThe number of characters to skip.
Returns
- StringSpan
A new StringSpan that skips the specified number of characters from the start.
Exceptions
- ArgumentOutOfRangeException
Thrown when the count is negative or greater than the current span length.
Take(int)
Creates a new StringSpan that represents a portion of the current span.
public StringSpan Take(int length)
Parameters
lengthintThe length of the new span.
Returns
- StringSpan
A new StringSpan that represents a portion of the current span.
Exceptions
- ArgumentOutOfRangeException
Thrown when the length is negative or greater than the current span length.
ToString()
public override string ToString()
Returns
TrimEnd()
Creates a new StringSpan with trailing whitespace removed.
public StringSpan TrimEnd()
Returns
- StringSpan
A new StringSpan with trailing whitespace removed.
TrimStart()
Creates a new StringSpan with leading whitespace removed.
public StringSpan TrimStart()
Returns
- StringSpan
A new StringSpan with leading whitespace removed.
Operators
operator ==(StringSpan, StringSpan)
Determines whether two StringSpan instances are equal.
public static bool operator ==(StringSpan span1, StringSpan span2)
Parameters
span1StringSpanThe first StringSpan to compare.
span2StringSpanThe second StringSpan to compare.
Returns
- bool
trueif the two StringSpan instances are equal; otherwise,false.
operator ==(StringSpan, ReadOnlySpan<char>)
Determines whether a StringSpan instance is equal to a read-only span of characters.
public static bool operator ==(StringSpan span1, ReadOnlySpan<char> span2)
Parameters
span1StringSpanThe StringSpan to compare.
span2ReadOnlySpan<char>The read-only span of characters to compare.
Returns
- bool
trueif the StringSpan instance is equal to the read-only span of characters; otherwise,false.
operator ==(ReadOnlySpan<char>, StringSpan)
Determines whether a read-only span of characters is equal to a StringSpan instance.
public static bool operator ==(ReadOnlySpan<char> span1, StringSpan span2)
Parameters
span1ReadOnlySpan<char>The read-only span of characters to compare.
span2StringSpanThe StringSpan to compare.
Returns
- bool
trueif the read-only span of characters is equal to the StringSpan instance; otherwise,false.
implicit operator ReadOnlySpan<char>(StringSpan)
Implicitly converts a StringSpan to a read-only span of characters.
public static implicit operator ReadOnlySpan<char>(StringSpan span)
Parameters
spanStringSpanThe StringSpan to convert.
Returns
- ReadOnlySpan<char>
A read-only span of characters.
operator !=(StringSpan, StringSpan)
Determines whether two StringSpan instances are not equal.
public static bool operator !=(StringSpan span1, StringSpan span2)
Parameters
span1StringSpanThe first StringSpan to compare.
span2StringSpanThe second StringSpan to compare.
Returns
- bool
trueif the two StringSpan instances are not equal; otherwise,false.
operator !=(StringSpan, ReadOnlySpan<char>)
Determines whether a StringSpan instance is not equal to a read-only span of characters.
public static bool operator !=(StringSpan span1, ReadOnlySpan<char> span2)
Parameters
span1StringSpanThe StringSpan to compare.
span2ReadOnlySpan<char>The read-only span of characters to compare.
Returns
- bool
trueif the StringSpan instance is not equal to the read-only span of characters; otherwise,false.
operator !=(ReadOnlySpan<char>, StringSpan)
Determines whether a read-only span of characters is not equal to a StringSpan instance.
public static bool operator !=(ReadOnlySpan<char> span1, StringSpan span2)
Parameters
span1ReadOnlySpan<char>The read-only span of characters to compare.
span2StringSpanThe StringSpan to compare.
Returns
- bool
trueif the read-only span of characters is not equal to the StringSpan instance; otherwise,false.