Repository navigation
OnixLabs.Core.StringExtensions
Extension methods for string instances, covering nth-occurrence indexing, delimiter-based substring extraction, repetition, wrapping, encoding/parsing conversions, and escaping.
| Namespace | OnixLabs.Core |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → StringExtensions
|
[EditorBrowsable(EditorBrowsableState.Never)]
public static class StringExtensionsNote This class is decorated with
[EditorBrowsable(EditorBrowsableState.Never)], which keeps the container class itself out of IntelliSense lists while its extension methods remain fully usable on the receiver types. Bring the methods into scope withusing OnixLabs.Core;.
All comparison-based methods default to StringComparison.Ordinal for predictable, culture-independent matching. Pass an explicit StringComparison argument to any overload that accepts one to override this behaviour.
| Method | Returns | Description |
|---|---|---|
NthIndexOf(char, int) |
int |
Index of the nth occurrence of a character, or -1. |
NthIndexOf(string, int, StringComparison) |
int |
Index of the nth occurrence of a substring, or -1. |
Repeat(int) |
string |
Repeats the string the specified number of times. |
SubstringBeforeFirst(char, string?) |
string |
Sub-string before the first occurrence of a character. |
SubstringBeforeFirst(string, string?, StringComparison) |
string |
Sub-string before the first occurrence of a substring. |
SubstringBeforeLast(char, string?) |
string |
Sub-string before the last occurrence of a character. |
SubstringBeforeLast(string, string?, StringComparison) |
string |
Sub-string before the last occurrence of a substring. |
SubstringAfterFirst(char, string?) |
string |
Sub-string after the first occurrence of a character. |
SubstringAfterFirst(string, string?, StringComparison) |
string |
Sub-string after the first occurrence of a substring. |
SubstringAfterLast(char, string?) |
string |
Sub-string after the last occurrence of a character. |
SubstringAfterLast(string, string?, StringComparison) |
string |
Sub-string after the last occurrence of a substring. |
SubstringBeforeNth(char, int, string?) |
string |
Sub-string before the nth occurrence of a character. |
SubstringBeforeNth(string, int, string?, StringComparison) |
string |
Sub-string before the nth occurrence of a substring. |
SubstringAfterNth(char, int, string?) |
string |
Sub-string after the nth occurrence of a character. |
SubstringAfterNth(string, int, string?, StringComparison) |
string |
Sub-string after the nth occurrence of a substring. |
ToByteArray(Encoding?) |
byte[] |
Encodes the string into a byte[]. |
ToDateTime(IFormatProvider?, DateTimeStyles) |
DateTime |
Parses the string into a DateTime. |
ToDateOnly(IFormatProvider?, DateTimeStyles) |
DateOnly |
Parses the string into a DateOnly. |
ToTimeOnly(IFormatProvider?, DateTimeStyles) |
TimeOnly |
Parses the string into a TimeOnly. |
ToEscapedString() |
string |
Formats escape characters as escape character literals. |
TryCopyTo(Span<char>, out int) |
bool |
Tries to copy the string into a destination span. |
Wrap(char, char) |
string |
Wraps the string between two characters. |
Wrap(string, string) |
string |
Wraps the string between two substrings. |
public int NthIndexOf(char seekValue, int count)Obtains the zero-based index of the nth occurrence of the specified character in this instance. Returns -1 if the receiver is null or empty, if count is less than or equal to zero, or if the specified occurrence does not exist.
| Parameter | Type | Description |
|---|---|---|
seekValue |
char |
The character to seek. |
count |
int |
The 1-based occurrence number to locate. |
Returns — The zero-based index of the nth occurrence of seekValue, if found; otherwise, -1.
int index = "a.b.c.d".NthIndexOf('.', 2); // 3public int NthIndexOf(string seekValue, int count, StringComparison comparison = StringComparison.Ordinal)Obtains the zero-based index of the nth occurrence of the specified substring in this instance. Returns -1 if the receiver or seekValue is null or empty, if count is less than or equal to zero, or if the specified occurrence does not exist.
| Parameter | Type | Description |
|---|---|---|
seekValue |
string |
The substring to seek. |
count |
int |
The 1-based occurrence number to locate. |
comparison |
StringComparison |
The comparison used to compare the receiver and seekValue. Defaults to StringComparison.Ordinal. |
Returns — The zero-based index of the nth occurrence of seekValue, if found; otherwise, -1.
public string Repeat(int count)Repeats the current string by the specified number of repetitions. Returns string.Empty when count is less than or equal to zero.
| Parameter | Type | Description |
|---|---|---|
count |
int |
The number of repetitions of the current string. |
Returns — A new string representing the repetition of the current string.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The length of the result would exceed int.MaxValue. |
string line = "ab".Repeat(3); // "ababab"public string SubstringBeforeFirst(char delimiter, string? defaultValue = null)Obtains a sub-string before the first occurrence of the specified delimiter.
| Parameter | Type | Description |
|---|---|---|
delimiter |
char |
The delimiter to find within the current string. |
defaultValue |
string? |
The value to return when the delimiter is not found. If null, the current string is returned. |
Returns — The sub-string before the first occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.
string name = "user@example.com".SubstringBeforeFirst('@'); // "user"public string SubstringBeforeFirst(string delimiter, string? defaultValue = null, StringComparison comparison = StringComparison.Ordinal)Obtains a sub-string before the first occurrence of the specified delimiter substring.
| Parameter | Type | Description |
|---|---|---|
delimiter |
string |
The delimiter to find within the current string. |
defaultValue |
string? |
The value to return when the delimiter is not found. If null, the current string is returned. |
comparison |
StringComparison |
The culture, case and sort rules applied when searching. Defaults to StringComparison.Ordinal. |
Returns — The sub-string before the first occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.
public string SubstringBeforeLast(char delimiter, string? defaultValue = null)Obtains a sub-string before the last occurrence of the specified delimiter.
| Parameter | Type | Description |
|---|---|---|
delimiter |
char |
The delimiter to find within the current string. |
defaultValue |
string? |
The value to return when the delimiter is not found. If null, the current string is returned. |
Returns — The sub-string before the last occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.
string path = "a/b/c/file.txt".SubstringBeforeLast('/'); // "a/b/c"public string SubstringBeforeLast(string delimiter, string? defaultValue = null, StringComparison comparison = StringComparison.Ordinal)Obtains a sub-string before the last occurrence of the specified delimiter substring.
| Parameter | Type | Description |
|---|---|---|
delimiter |
string |
The delimiter to find within the current string. |
defaultValue |
string? |
The value to return when the delimiter is not found. If null, the current string is returned. |
comparison |
StringComparison |
The culture, case and sort rules applied when searching. Defaults to StringComparison.Ordinal. |
Returns — The sub-string before the last occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.
public string SubstringAfterFirst(char delimiter, string? defaultValue = null)Obtains a sub-string after the first occurrence of the specified delimiter.
| Parameter | Type | Description |
|---|---|---|
delimiter |
char |
The delimiter to find within the current string. |
defaultValue |
string? |
The value to return when the delimiter is not found. If null, the current string is returned. |
Returns — The sub-string after the first occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.
string domain = "user@example.com".SubstringAfterFirst('@'); // "example.com"public string SubstringAfterFirst(string delimiter, string? defaultValue = null, StringComparison comparison = StringComparison.Ordinal)Obtains a sub-string after the first occurrence of the specified delimiter substring.
| Parameter | Type | Description |
|---|---|---|
delimiter |
string |
The delimiter to find within the current string. |
defaultValue |
string? |
The value to return when the delimiter is not found. If null, the current string is returned. |
comparison |
StringComparison |
The culture, case and sort rules applied when searching. Defaults to StringComparison.Ordinal. |
Returns — The sub-string after the first occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.
public string SubstringAfterLast(char delimiter, string? defaultValue = null)Obtains a sub-string after the last occurrence of the specified delimiter.
| Parameter | Type | Description |
|---|---|---|
delimiter |
char |
The delimiter to find within the current string. |
defaultValue |
string? |
The value to return when the delimiter is not found. If null, the current string is returned. |
Returns — The sub-string after the last occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.
string extension = "archive.tar.gz".SubstringAfterLast('.'); // "gz"public string SubstringAfterLast(string delimiter, string? defaultValue = null, StringComparison comparison = StringComparison.Ordinal)Obtains a sub-string after the last occurrence of the specified delimiter substring.
| Parameter | Type | Description |
|---|---|---|
delimiter |
string |
The delimiter to find within the current string. |
defaultValue |
string? |
The value to return when the delimiter is not found. If null, the current string is returned. |
comparison |
StringComparison |
The culture, case and sort rules applied when searching. Defaults to StringComparison.Ordinal. |
Returns — The sub-string after the last occurrence of delimiter; otherwise, defaultValue or the current string when defaultValue is null.
public string SubstringBeforeNth(char seekValue, int count, string? defaultValue = null)Obtains a sub-string before the nth occurrence of the specified character. If the nth occurrence is not found, returns defaultValue, or the entire string when defaultValue is null.
| Parameter | Type | Description |
|---|---|---|
seekValue |
char |
The character to find the nth occurrence of. |
count |
int |
The nth occurrence to find. |
defaultValue |
string? |
The value to return if the nth occurrence is not found. If null, the current string is returned. |
Returns — The sub-string before the nth occurrence of seekValue if found; otherwise, defaultValue or the entire string when defaultValue is null.
string head = "a.b.c.d".SubstringBeforeNth('.', 2); // "a.b"public string SubstringBeforeNth(string seekValue, int count, string? defaultValue = null, StringComparison comparison = StringComparison.Ordinal)Obtains a sub-string before the nth occurrence of the specified substring. If the nth occurrence is not found, returns defaultValue, or the entire string when defaultValue is null.
| Parameter | Type | Description |
|---|---|---|
seekValue |
string |
The substring to find the nth occurrence of. |
count |
int |
The nth occurrence to find. |
defaultValue |
string? |
The value to return if the nth occurrence is not found. If null, the current string is returned. |
comparison |
StringComparison |
The comparison used to compare the receiver and seekValue. Defaults to StringComparison.Ordinal. |
Returns — The sub-string before the nth occurrence of seekValue if found; otherwise, defaultValue or the entire string when defaultValue is null.
public string SubstringAfterNth(char seekValue, int count, string? defaultValue = null)Obtains a sub-string after the nth occurrence of the specified character. If the nth occurrence is not found, returns defaultValue, or the entire string when defaultValue is null.
| Parameter | Type | Description |
|---|---|---|
seekValue |
char |
The character to find the nth occurrence of. |
count |
int |
The nth occurrence to find. |
defaultValue |
string? |
The value to return if the nth occurrence is not found. If null, the current string is returned. |
Returns — The sub-string after the nth occurrence of seekValue if found; otherwise, defaultValue or the entire string when defaultValue is null.
string tail = "a.b.c.d".SubstringAfterNth('.', 2); // "c.d"public string SubstringAfterNth(string seekValue, int count, string? defaultValue = null, StringComparison comparison = StringComparison.Ordinal)Obtains a sub-string after the nth occurrence of the specified substring. If the nth occurrence is not found, returns defaultValue, or the entire string when defaultValue is null.
| Parameter | Type | Description |
|---|---|---|
seekValue |
string |
The substring to find the nth occurrence of. |
count |
int |
The nth occurrence to find. |
defaultValue |
string? |
The value to return if the nth occurrence is not found. If null, the current string is returned. |
comparison |
StringComparison |
The comparison used to compare the receiver and seekValue. Defaults to StringComparison.Ordinal. |
Returns — The sub-string after the nth occurrence of seekValue if found; otherwise, defaultValue or the entire string when defaultValue is null.
public byte[] ToByteArray(Encoding? encoding = null)Converts the current string instance into a new byte[] array. When encoding is null, a default encoding is used.
| Parameter | Type | Description |
|---|---|---|
encoding |
Encoding? |
The encoding used to convert the current string into a byte[]. Defaults to null. |
Returns — A new byte[] representation of the current string.
byte[] bytes = "Hello".ToByteArray(Encoding.UTF8);public DateTime ToDateTime(IFormatProvider? provider = null, DateTimeStyles styles = DateTimeStyles.None)Converts the current string instance into a new DateTime instance via DateTime.Parse.
| Parameter | Type | Description |
|---|---|---|
provider |
IFormatProvider? |
An object that provides culture-specific formatting information. Defaults to null. |
styles |
DateTimeStyles |
A bit-wise combination of values that indicate the permitted style elements. Defaults to DateTimeStyles.None. |
Returns — A new DateTime instance parsed from the current string.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
The current string is not a recognised DateTime. |
public DateOnly ToDateOnly(IFormatProvider? provider = null, DateTimeStyles styles = DateTimeStyles.None)Converts the current string instance into a new DateOnly instance via DateOnly.Parse.
| Parameter | Type | Description |
|---|---|---|
provider |
IFormatProvider? |
An object that provides culture-specific formatting information. Defaults to null. |
styles |
DateTimeStyles |
A bit-wise combination of values that indicate the permitted style elements. Defaults to DateTimeStyles.None. |
Returns — A new DateOnly instance parsed from the current string.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
The current string is not a recognised DateOnly. |
public TimeOnly ToTimeOnly(IFormatProvider? provider = null, DateTimeStyles styles = DateTimeStyles.None)Converts the current string instance into a new TimeOnly instance via TimeOnly.Parse.
| Parameter | Type | Description |
|---|---|---|
provider |
IFormatProvider? |
An object that provides culture-specific formatting information. Defaults to null. |
styles |
DateTimeStyles |
A bit-wise combination of values that indicate the permitted style elements. Defaults to DateTimeStyles.None. |
Returns — A new TimeOnly instance parsed from the current string.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
The current string is not a recognised TimeOnly. |
public string ToEscapedString()Obtains a string with all escape characters formatted as escape character literals. Newline, carriage return, tab, double quote, single quote and backslash are rendered as their escape sequences; other control characters are rendered as \uXXXX.
Returns — A string with all escape characters formatted as escape character literals.
string escaped = "line1\nline2\t\"quoted\"".ToEscapedString();
// "line1\\nline2\\t\\\"quoted\\\""public bool TryCopyTo(Span<char> destination, out int charsWritten)Tries to copy the current string instance into the destination Span<char>.
| Parameter | Type | Description |
|---|---|---|
destination |
Span<char> |
The span into which the current string contents are copied. |
charsWritten |
out int |
The number of characters written to destination; 0 when the copy fails. |
Returns — true if the current string was copied into destination; otherwise, false if the destination was too short.
public string Wrap(char before, char after)Wraps the current string instance between the specified before and after char instances.
| Parameter | Type | Description |
|---|---|---|
before |
char |
The char that should precede the current string. |
after |
char |
The char that should succeed the current string. |
Returns — A new string representing the current string wrapped between before and after.
string wrapped = "value".Wrap('[', ']'); // "[value]"public string Wrap(string before, string after)Wraps the current string instance between the specified before and after string instances.
| Parameter | Type | Description |
|---|---|---|
before |
string |
The string that should precede the current string. |
after |
string |
The string that should succeed the current string. |
Returns — A new string representing the current string wrapped between before and after.
string wrapped = "value".Wrap("<<", ">>"); // "<<value>>"- OnixLabs.Core
- Home
- OnixLabs.Core.Linq
- Text
- OnixLabs.Core.ArrayExtensions
- OnixLabs.Core.DateTimeExtensions
- OnixLabs.Core.ComparableExtensions
- OnixLabs.Core.ISpanParsableExtensions
- OnixLabs.Core.ObjectExtensions
- OnixLabs.Core.RandomExtensions
- OnixLabs.Core.ReadOnlySequenceExtensions
- OnixLabs.Core.ReadOnlySpanExtensions
- OnixLabs.Core on NuGet
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions