KGy SOFT

StringSegment Structure

KGy SOFT Core Libraries Help
Represents a segment of a string. This type is similar to ReadOnlyMemory<char>/ReadOnlySpan<char> but StringSegment can be used in all platforms in the same way and is optimized for some dedicated string operations.
To create an instance use the AsSegment extension method overloads or just cast a string instance to StringSegment.
See the Remarks section for details.

Namespace:  KGySoft.CoreLibraries
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.3.0
Syntax

[SerializableAttribute]
[TypeConverterAttribute(typeof(StringSegmentConverter))]
public readonly struct StringSegment : IEquatable<StringSegment>, 
	IComparable<StringSegment>, IComparable, IReadOnlyList<char>, IEnumerable<char>, 
	IEnumerable, IReadOnlyCollection<char>

The StringSegment type exposes the following members.

Properties

  NameDescription
Public propertyAsMemory
Returns the current StringSegment instance as a ReadOnlyMemory of characters.
Public propertyAsSpan
Returns the current StringSegment instance as a ReadOnlySpan of characters.
Public propertyIsNull
Gets whether this StringSegment instance was created from a  string.
Please note that the ToString method returns  when this property returns .
Public propertyIsNullOrEmpty
Gets whether this StringSegment instance represents an empty segment or was created from a  string.
Public propertyIsNullOrWhiteSpace
Gets whether this StringSegment instance represents a  or empty string, or contains only whitespace characters.
Public propertyItemInt32
Gets the character at the specified position in this StringSegment.
Public propertyItemRange
Gets the StringSegment from this instance that represents the substring of the specified range.
Public propertyLength
Gets the length of this StringSegment.
Public propertyOffset
Gets the offset, which denotes the start position of this StringSegment within the UnderlyingString.
Public propertyUnderlyingString
Gets the underlying string of this StringSegment.
Top
Methods

  NameDescription
Public methodStatic memberCompare(StringSegment, StringSegment, StringComparison)
Compares two specified StringSegment instances using the specified comparison, and returns an integer that indicates their relative position in the sort order.
Public methodStatic memberCompare(StringSegment, StringSegment, Boolean, CultureInfo)
Compares two specified StringSegment instances, ignoring or honoring their case, and using the specified culture, and returns an integer that indicates their relative position in the sort order.
Public methodCompareTo(Object)
Compares this instance to the specified object using ordinal comparison, and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified StringSegment.
Public methodCompareTo(StringSegment)
Compares this instance to the specified StringSegment using ordinal comparison, and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified StringSegment.
Public methodEndsWith(Char)
Gets whether this StringSegment instance ends with the specified value.
Public methodEndsWith(ReadOnlySpanChar, StringComparison)
Gets whether this StringSegment instance ends with the specified value using the specified comparison.
Public methodEndsWith(StringSegment, StringComparison)
Gets whether this StringSegment instance ends with the specified value using the specified comparison.
Public methodEquals(Object)
Determines whether the specified object is equal to this instance.
(Overrides ValueTypeEquals(Object).)
Public methodEquals(String)
Indicates whether the current StringSegment instance is equal to another one specified in the other parameter.
Public methodEquals(StringSegment)
Indicates whether the current StringSegment instance is equal to another one specified in the other parameter.
Public methodStatic memberEquals(StringSegment, StringSegment, StringComparison)
Determines whether two specified StringSegment instances have the same value using the specified comparison.
Public methodGetEnumerator
Returns an enumerator that iterates through the StringSegment characters.
Public methodGetHashCode
Returns a hash code for this StringSegment instance.
(Overrides ValueTypeGetHashCode.)
Public methodGetHashCode(StringComparison)
Returns the hash code for this StringSegment using the specified comparison.
Public methodIndexOf(Char)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment.
Public methodIndexOf(ReadOnlySpanChar)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using ordinal comparison.
Public methodIndexOf(String)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using ordinal comparison.
Public methodIndexOf(StringSegment)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using ordinal comparison.
Public methodIndexOf(Char, Int32)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using the specified startIndex.
Public methodIndexOf(ReadOnlySpanChar, StringComparison)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using the specified comparison.
Public methodIndexOf(String, StringComparison)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using the specified comparison.
Public methodIndexOf(StringSegment, StringComparison)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using the specified comparison.
Public methodIndexOf(Char, Int32, Int32)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using the specified startIndex and count values.
Public methodIndexOf(ReadOnlySpanChar, Int32, StringComparison)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using the specified startIndex and comparison.
Public methodIndexOf(String, Int32, StringComparison)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using the specified startIndex and comparison.
Public methodIndexOf(StringSegment, Int32, StringComparison)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using the specified startIndex and comparison.
Public methodIndexOf(ReadOnlySpanChar, Int32, Int32, StringComparison)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using the specified startIndex, count and comparison.
Public methodIndexOf(String, Int32, Int32, StringComparison)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using the specified startIndex, count and comparison.
Public methodIndexOf(StringSegment, Int32, Int32, StringComparison)
Gets the zero-based index of the first occurrence of the specified value in this StringSegment using the specified startIndex, count and comparison.
Public methodIndexOfAny(Char)
Gets the zero-based index of the first occurrence in this StringSegment of any character in the specified array.
Public methodIndexOfAny(Char, Int32)
Gets the zero-based index of the first occurrence in this StringSegment of any character in the specified array using the specified startIndex.
Public methodIndexOfAny(Char, Int32, Int32)
Gets the zero-based index of the first occurrence in this StringSegment of any character in the specified array using the specified startIndex and count values.
Public methodLastIndexOf(Char)
Gets the zero-based index of the last occurrence of the specified value in this StringSegment.
Public methodLastIndexOf(Char, Int32)
Gets the zero-based index of the last occurrence of the specified value in this StringSegment using the specified startIndex.
Public methodLastIndexOf(ReadOnlySpanChar, StringComparison)
Gets the zero-based index of the last occurrence of the specified value in this StringSegment using the specified comparison.
Public methodLastIndexOf(String, StringComparison)
Gets the zero-based index of the last occurrence of the specified value in this StringSegment using the specified comparison.
Public methodLastIndexOf(StringSegment, StringComparison)
Gets the zero-based index of the last occurrence of the specified value in this StringSegment using the specified comparison.
Public methodLastIndexOf(Char, Int32, Int32)
Gets the zero-based index of the last occurrence of the specified value in this StringSegment using the specified startIndex and count values.
Public methodLastIndexOf(ReadOnlySpanChar, Int32, StringComparison)
Gets the zero-based index of the last occurrence of the specified value in this StringSegment using the specified startIndex and comparison.
Public methodLastIndexOf(String, Int32, StringComparison)
Gets the zero-based index of the last occurrence of the specified value in this StringSegment using the specified startIndex and comparison.
Public methodLastIndexOf(StringSegment, Int32, StringComparison)
Gets the zero-based index of the last occurrence of the specified value in this StringSegment using the specified startIndex and comparison.
Public methodLastIndexOf(ReadOnlySpanChar, Int32, Int32, StringComparison)
Gets the zero-based index of the last occurrence of the specified value in this StringSegment using the specified startIndex, count and comparison.
Public methodLastIndexOf(String, Int32, Int32, StringComparison)
Gets the zero-based index of the last occurrence of the specified value in this StringSegment using the specified startIndex, count and comparison.
Public methodLastIndexOf(StringSegment, Int32, Int32, StringComparison)
Gets the zero-based index of the last occurrence of the specified value in this StringSegment using the specified startIndex, count and comparison.
Public methodLastIndexOfAny(Char)
Gets the zero-based index of the last occurrence in this StringSegment of any character in the specified array.
Public methodLastIndexOfAny(Char, Int32)
Gets the zero-based index of the last occurrence in this StringSegment of any character in the specified array using the specified startIndex.
Public methodLastIndexOfAny(Char, Int32, Int32)
Gets the zero-based index of the last occurrence in this StringSegment of any character in the specified array using the specified startIndex and count values.
Public methodSplit(Boolean)
Splits this StringSegment instance into a collection of StringSegment instances without allocating new strings. This overload uses the whitespace characters as separators. Alternatively, you can use the ReadToWhiteSpace extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(Char)
Splits this StringSegment instance into a collection of StringSegment instances without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, Char) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(String)
Splits this StringSegment instance into a collection of StringSegment instances without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, String) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(StringSegment)
Splits this StringSegment instance into a collection of StringSegment instances without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, StringSegment) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(Char, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, Char) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(Char, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, Char) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(NullableInt32, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances of no more than maxLength segments, without allocating new strings. This overload uses the whitespace characters as separators. Alternatively, you can use the ReadToWhiteSpace extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(ReadOnlySpanChar, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, ReadOnlySpanChar) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(String, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, String) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(String, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, String) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(StringSegment, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, StringSegment) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(StringSegment, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, StringSegment) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(Char, NullableInt32, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances of no more than maxLength segments, without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, Char) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(Char, NullableInt32, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances of no more than maxLength segments, without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, Char) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(ReadOnlySpanChar, NullableInt32, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances of no more than maxLength segments, without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, ReadOnlySpanChar) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(String, NullableInt32, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances of no more than maxLength segments, without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, String) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(String, NullableInt32, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances of no more than maxLength segments, without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, String) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(StringSegment, NullableInt32, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances of no more than maxLength segments, without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, StringSegment) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodSplit(StringSegment, NullableInt32, Boolean)
Splits this StringSegment instance into a collection of StringSegment instances of no more than maxLength segments, without allocating new strings. Alternatively, you can use the ReadToSeparator(StringSegment, StringSegment) extension method.
See the Remarks section of the StringSegment type for details and some examples.
Public methodStartsWith(Char)
Gets whether this StringSegment instance starts with the specified value.
Public methodStartsWith(ReadOnlySpanChar, StringComparison)
Gets whether this StringSegment instance starts with the specified value using the specified comparison.
Public methodStartsWith(String, StringComparison)
Gets whether this StringSegment instance starts with the specified value using the specified comparison.
Public methodStartsWith(StringSegment, StringComparison)
Gets whether this StringSegment instance starts with the specified value using the specified comparison.
Public methodSubstring(Int32)
Gets a new StringSegment instance, which represents a substring of the current instance with the specified startIndex.
Public methodSubstring(Int32, Int32)
Gets a StringSegment instance, which represents a substring of the current instance with the specified startIndex and length.
Public methodToString
Gets a string that is represented by this StringSegment instance, or , if this instance represents a  string. That is, when the IsNull property returns .
(Overrides ValueTypeToString.)
Public methodTrim
Removes all leading and trailing white-space characters from the current StringSegment.
Public methodTrim(Char)
Removes all leading and trailing instances of a character from the current StringSegment.
Public methodTrim(Char)
Removes all leading and trailing occurrences of a set of characters specified in an array from the current StringSegment.
Public methodTrim(ReadOnlySpanChar)
Removes all leading and trailing occurrences of a set of characters specified in an array from the current StringSegment.
Public methodTrimEnd
Removes all the trailing white-space characters from the current StringSegment.
Public methodTrimEnd(Char)
Removes all trailing instances of a character from the current StringSegment.
Public methodTrimEnd(Char)
Removes all trailing occurrences of a set of characters specified in an array from the current StringSegment.
Public methodTrimEnd(ReadOnlySpanChar)
Removes all trailing occurrences of a set of characters specified in an array from the current StringSegment.
Public methodTrimStart
Removes all the leading white-space characters from the current StringSegment.
Public methodTrimStart(Char)
Removes all leading instances of a character from the current StringSegment.
Public methodTrimStart(Char)
Removes all leading occurrences of a set of characters specified in an array from the current StringSegment.
Public methodTrimStart(ReadOnlySpanChar)
Removes all leading occurrences of a set of characters specified in an array from the current StringSegment.
Top
Operators

  NameDescription
Public operatorStatic memberEquality
Determines whether two specified StringSegment instances have the same value.
Public operatorStatic member(StringSegment to String)
Performs an explicit conversion from StringSegment to string.
Public operatorStatic member(String to StringSegment)
Performs an implicit conversion from string to StringSegment.
Public operatorStatic member(StringSegment to ReadOnlySpanChar)
Performs an implicit conversion from StringSegment to ReadOnlySpan<char>.
Public operatorStatic memberInequality
Determines whether two specified StringSegment instances have different values.
Top
Fields

  NameDescription
Public fieldStatic memberEmpty
Represents the empty StringSegment. This field is read-only.
Public fieldStatic memberNull
Represents the  StringSegment. This field is read-only.
Top
Extension Methods

  NameDescription
Public Extension MethodConvert(Type, CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions.)
Public Extension MethodCode exampleConvertTTarget(CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)
Public Extension MethodForEachChar
Similarly to the List<T>.ForEach method, processes an action on each element of an enumerable collection.
(Defined by EnumerableExtensions.)
Public Extension MethodGetRandomElementChar(Boolean)Overloaded.
Gets a random element from the enumerable source using a new Random instance.
(Defined by EnumerableExtensions.)
Public Extension MethodGetRandomElementChar(Random, Boolean)Overloaded.
Gets a random element from the enumerable source using a specified Random instance.
(Defined by EnumerableExtensions.)
Public Extension MethodIn (Defined by ObjectExtensions.)
Public Extension MethodIndexOf(FuncObject, Boolean)Overloaded.
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions.)
Public Extension MethodIndexOf(Object)Overloaded.
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions.)
Public Extension MethodIndexOfChar(FuncChar, Boolean)Overloaded.
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions.)
Public Extension MethodIndexOfChar(Char)Overloaded.
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions.)
Public Extension MethodIsNullOrEmptyOverloaded.
Determines whether the specified source is  or empty (has no elements).
(Defined by EnumerableExtensions.)
Public Extension MethodIsNullOrEmptyCharOverloaded.
Determines whether the specified source is  or empty (has no elements).
(Defined by EnumerableExtensions.)
Public Extension MethodJoinChar(String)Overloaded.
Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions.)
Public Extension MethodJoinChar(Char)Overloaded.
Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions.)
Public Extension MethodRemoveQuotes
Extracts content of a single or double quoted string.
(Defined by StringSegmentExtensions.)
Public Extension MethodShuffleCharOverloaded.
Shuffles an enumerable source (randomizes its elements) using a new Random instance.
(Defined by EnumerableExtensions.)
Public Extension MethodShuffleChar(Int32)Overloaded.
Shuffles an enumerable source (randomizes its elements) using the provided seed with a new Random instance.
(Defined by EnumerableExtensions.)
Public Extension MethodShuffleChar(Random)Overloaded.
Shuffles an enumerable source (randomizes its elements) using a specified Random instance.
(Defined by EnumerableExtensions.)
Public Extension MethodToCircularListChar (Defined by EnumerableExtensions.)
Public Extension MethodToEnumTEnum
Tries to convert the specified StringSegment to an Enum value of TEnum type. No string allocation occurs when using this method.
(Defined by StringSegmentExtensions.)
Public Extension MethodTryAdd(Object, Boolean, Boolean)Overloaded.
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryAddChar(Char, Boolean, Boolean)Overloaded.
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryAddRange(IEnumerable, Boolean, Boolean)Overloaded.
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryAddRangeChar(IEnumerableChar, Boolean, Boolean)Overloaded.
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryClear(Boolean, Boolean)Overloaded.
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryClearChar(Boolean, Boolean)Overloaded.
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryConvert(Type, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, CultureInfo, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(CultureInfo, TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryGetElementAt(Int32, Object, Boolean, Boolean)Overloaded.
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryGetElementAtChar(Int32, Char, Boolean, Boolean)Overloaded.
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsert(Int32, Object, Boolean, Boolean)Overloaded.
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsertChar(Int32, Char, Boolean, Boolean)Overloaded.
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsertRange(Int32, IEnumerable, Boolean, Boolean)Overloaded.
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsertRangeChar(Int32, IEnumerableChar, Boolean, Boolean)Overloaded.
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemove(Object, Boolean, Boolean)Overloaded.
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveChar(Char, Boolean, Boolean)Overloaded.
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveAt(Int32, Boolean, Boolean)Overloaded.
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveAtChar(Int32, Boolean, Boolean)Overloaded.
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveRange(Int32, Int32, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveRangeChar(Int32, Int32, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions.)
Public Extension MethodTryReplaceRange(Int32, Int32, IEnumerable, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the target at the specified index, and to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions.)
Public Extension MethodTryReplaceRangeChar(Int32, Int32, IEnumerableChar, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the target at the specified index, and to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions.)
Public Extension MethodTrySetElementAt(Int32, Object, Boolean, Boolean)Overloaded.
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTrySetElementAtChar(Int32, Char, Boolean, Boolean)Overloaded.
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Top
Remarks

To create a StringSegment instance from a string you can use the implicit conversion, or the AsSegment extension methods.

To convert a StringSegment instance to string use an explicit cast or the ToString method.

Note Note
The StringSegment type may outperform string in scenarios when usual string splitting/trimming operations would allocate long strings.
See a live example with performance test here.

Depending on the used platform some members of the StringSegment type may allocate a new string. The affected members are:

Note Note
On .NET Core 3.0 and newer platforms none of the members above allocate a new string. On .NET Standard 2.1 and newer platforms the IndexOf overloads are not affected.

As opposed to the String class, the default comparison strategy in StringSegment members is Ordinal.

Examples

The following example demonstrates how to use the StringSegment type:

C#
using System;
using KGySoft.CoreLibraries;

class Example
{
    public static void Main()
    {
        // Assignment works from string
        StringSegment segment = "Some string literal";

        // Or by extension methods:
        segment = "Some string literal".AsSegment(); // "Some string literal"
        segment = "Some string literal".AsSegment(0, 4); // "Some"
        segment = "Some string literal".AsSegment(5, 6); // "string"

        // Null assignment: all the following lines have the same effect:
        segment = default(StringSegment); // the fastest way
        segment = StringSegment.Null; // the recommended way
        segment = null; // the cleanest way - same as segment = ((string)null).AsSegment()

        // Null check (remember, StringSegment is a value type with null semantics):
        bool isNull = segment == null; // the cleanest way - same as segment.Equals(((string)null).AsSegment())
        isNull = segment.IsNull; // the fastest and recommended way - same as segment.UnderlyingString == null

        // Slicing:
        segment = "Some string literal";
        Console.WriteLine(segment.Substring(0, 4)); // "Some"
        Console.WriteLine(segment.Substring(5)); // "string literal"
        Console.WriteLine(segment.Split(' ').Count); // 3
        Console.WriteLine(segment.Split(' ')[2]); // "literal"

        // Slicing operations do not allocate new strings:
        StringSegment subsegment = segment.Substring(5);
        subsegment = segment[5..]; // Range indexer is also supported
        Console.WriteLine(subsegment); // "string literal"
        Console.WriteLine(subsegment.UnderlyingString); // "Some string literal"

        // As StringSegment can be implicitly converted to ReadOnlySpan<char> it can be passed
        // to many already existing API accepting spans (in .NET Core 3.0/.NET Standard 2.1 and above):
        int parsedResult = Int32.Parse("Value=42".AsSegment().Split('=')[1]);
    }
}

The following example demonstrates a possible usage of the StringSegment type:

Tip Tip
Try the extended example with performance comparison online.
C#
/**************************************************************
 * This example retrieves values from multiline text like this:
 * Key1=Value1;Value2
 * Key2=SingleValue
 * See a working example here: https://dotnetfiddle.net/Byk0YM
 **************************************************************/
// The original way:
public static string[] ByString(string content, string key)
{
    // getting all lines, filtering the first empty line
    string[] nonEmptyLines = content.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

    foreach (string line in nonEmptyLines)
    {
        // Separating key from values. We can use count: 2 because we split at the first '=' only.
        string[] keyValues = line.Split(new[] { '=' }, count: 2);

        // Removing white spaces and returning values if the key matches
        if (keyValues[0].TrimStart() == key)
            return keyValues[1].Split(';');
    }

    // key not found
    return null;
}

// The StringSegment way: almost the same code as above
public static IList<StringSegment> ByStringSegment(string content, string key)
{
    // getting all lines, filtering the first empty line
    IList<StringSegment> nonEmptyLines = content.AsSegment().Split(Environment.NewLine, removeEmptyEntries: true);

    foreach (StringSegment line in nonEmptyLines)
    {
        // Separating key from values. We can use maxLength: 2 because we split at the first '=' only.
        IList<StringSegment> keyValues = line.Split('=', maxLength: 2);

        // Removing white spaces and returning values if the key matches
        if (keyValues[0].TrimStart() == key)
            return keyValues[1].Split(';');
    }

    // key not found
    return null;
}

// An alternative StringSegment way: uses Split only if we need all segments or we can limit max counts.
public static IList<StringSegment> ByStringSegmentAlternative(string content, string key)
{
    StringSegment rest = content; // same as content.AsSegment()
    while (!rest.IsNull)
    {
        // Advancing to the next line (StringSegment is immutable but the extension uses ref this parameter)
        StringSegment line = rest.ReadLine(); // or ReadToSeparator(Environment.NewLine, "\r", "\n")
        if (line.Length == 0)
            continue;

        // Separating key from values. We can use maxLength: 2 because we split at the first '=' only.
        IList<StringSegment> keyValues = line.Split('=', maxLength: 2);

        // Removing white spaces and returning values if the key matches
        if (keyValues[0].TrimStart() == key)
            return keyValues[1].Split(';');
    }

    // key not found
    return null;
}

See Also

Reference