Table of Contents

Class StringExtensions

Namespace
DotNetExtensionKit
Assembly
DotNetExtensionKit.dll

Provides extension methods for string manipulation, validation, transformation, and encoding.

public static class StringExtensions
Inheritance
StringExtensions
Inherited Members

Methods

AlphaNumericOnly(string)

Returns a string containing only letter and digit characters from the input, preserving their original order.

public static string AlphaNumericOnly(this string value)

Parameters

value string

The source string to filter.

Returns

string

A string containing only the letter and digit characters from value.

Examples

string result = "hello world! 123".AlphaNumericOnly(); // "helloworld123"

CollapseWhitespace(string)

Replaces consecutive whitespace characters with a single space.

public static string CollapseWhitespace(this string value)

Parameters

value string

The source string.

Returns

string

A new string where runs of consecutive whitespace in value are replaced by a single space.

Examples

string result = "hello   world".CollapseWhitespace(); // "hello world"

DigitsOnly(string)

Returns a string containing only digit characters from the input, preserving their original order.

public static string DigitsOnly(this string value)

Parameters

value string

The source string to filter.

Returns

string

A string containing only the digit characters from value.

Examples

string result = "abc123def456".DigitsOnly(); // "123456"

EmptyIfNull(string?)

Returns an empty string if the string is null; otherwise returns the original string.

public static string EmptyIfNull(this string? value)

Parameters

value string

The string to test.

Returns

string

An empty string if value is null; otherwise the original string.

Examples

string result = ((string?)null).EmptyIfNull(); // ""
string result2 = "hello".EmptyIfNull(); // "hello"

FromBase64(string)

Decodes a Base64 string back to its original UTF-8 string representation.

public static string FromBase64(this string value)

Parameters

value string

The Base64-encoded string to decode.

Returns

string

The decoded UTF-8 string.

Examples

string result = "SGVsbG8=".FromBase64(); // "Hello"

IsNullOrEmpty(string?)

Returns true if the string is null or has zero length.

public static bool IsNullOrEmpty(this string? value)

Parameters

value string

The string to test.

Returns

bool

true if value is null or empty; otherwise false.

Examples

string? name = null;
bool result = name.IsNullOrEmpty(); // true
bool result2 = "hello".IsNullOrEmpty(); // false

IsNullOrWhiteSpace(string?)

Returns true if the string is null, empty, or contains only whitespace characters.

public static bool IsNullOrWhiteSpace(this string? value)

Parameters

value string

The string to test.

Returns

bool

true if value is null, empty, or whitespace-only; otherwise false.

Examples

bool result = "  ".IsNullOrWhiteSpace(); // true
bool result2 = "hello".IsNullOrWhiteSpace(); // false

Left(string, int)

Returns the first length characters of the string. If length exceeds the string length, returns the full string.

public static string Left(this string value, int length)

Parameters

value string

The source string.

length int

The number of characters to return from the start.

Returns

string

A string containing the first length characters, or the full string if it is shorter than length.

Examples

string result = "Hello, World!".Left(5); // "Hello"
string result2 = "Hi".Left(10); // "Hi"

Exceptions

ArgumentOutOfRangeException

Thrown when length is negative.

LettersOnly(string)

Returns a string containing only letter characters from the input, preserving their original order.

public static string LettersOnly(this string value)

Parameters

value string

The source string to filter.

Returns

string

A string containing only the letter characters from value.

Examples

string result = "abc123def456".LettersOnly(); // "abcdef"

Mask(string, int, int, char)

Masks the middle characters of a string, preserving the specified number of characters at the start and end. Returns null/empty input unchanged. Result length always equals input length.

public static string Mask(this string value, int unmaskedStart = 2, int unmaskedEnd = 2, char maskChar = '*')

Parameters

value string

The string to mask.

unmaskedStart int

Number of characters to preserve at the start.

unmaskedEnd int

Number of characters to preserve at the end.

maskChar char

The character used for masking.

Returns

string

A masked string with the middle characters replaced by maskChar, or the original string if it is null or empty.

Examples

string result = "1234567890".Mask(); // "12******90"
string result2 = "secret".Mask(1, 1, '#'); // "s####t"

NullIfEmpty(string?)

Returns null if the string is null or empty; otherwise returns the original string.

public static string? NullIfEmpty(this string? value)

Parameters

value string

The string to test.

Returns

string

The original string if it is not null or empty; otherwise null.

Examples

string? result = "".NullIfEmpty(); // null
string? result2 = "hello".NullIfEmpty(); // "hello"

NullIfWhiteSpace(string?)

Returns null if the string is null, empty, or contains only whitespace; otherwise returns the original string.

public static string? NullIfWhiteSpace(this string? value)

Parameters

value string

The string to test.

Returns

string

The original string if it contains at least one non-whitespace character; otherwise null.

Examples

string? result = "  ".NullIfWhiteSpace(); // null
string? result2 = "hello".NullIfWhiteSpace(); // "hello"

RemoveWhitespace(string)

Returns the string with all whitespace characters removed.

public static string RemoveWhitespace(this string value)

Parameters

value string

The source string.

Returns

string

A new string with all whitespace characters removed from value.

Examples

string result = "hello world".RemoveWhitespace(); // "helloworld"

Right(string, int)

Returns the last length characters of the string. If length exceeds the string length, returns the full string.

public static string Right(this string value, int length)

Parameters

value string

The source string.

length int

The number of characters to return from the end.

Returns

string

A string containing the last length characters, or the full string if it is shorter than length.

Examples

string result = "Hello, World!".Right(6); // "World!"
string result2 = "Hi".Right(10); // "Hi"

Exceptions

ArgumentOutOfRangeException

Thrown when length is negative.

SplitByCasing(string)

Inserts spaces at casing boundary positions in PascalCase or camelCase strings. Handles acronyms properly (e.g., "XMLParser" → "XML Parser").

public static string SplitByCasing(this string value)

Parameters

value string

The PascalCase or camelCase string to split.

Returns

string

A new string with spaces inserted before uppercase letters at casing boundaries.

Examples

string result = "HelloWorld".SplitByCasing(); // "Hello World"
string result2 = "XMLParser".SplitByCasing(); // "XML Parser"

SplitLines(string)

Returns an enumerable of strings split by line-break characters (\r\n, \r, \n).

public static IEnumerable<string> SplitLines(this string value)

Parameters

value string

The source string to split into lines.

Returns

IEnumerable<string>

An IEnumerable<T> of strings, one per line in value.

Examples

var lines = "line1\nline2\nline3".SplitLines(); // ["line1", "line2", "line3"]

ToBase64(string)

Returns the UTF-8 Base64-encoded representation of the string.

public static string ToBase64(this string value)

Parameters

value string

The string to encode.

Returns

string

A Base64-encoded string representing the UTF-8 bytes of value.

Examples

string result = "Hello".ToBase64(); // "SGVsbG8="

ToCamelCase(string)

Returns a camelCase string: first character lowercase, subsequent word-initial characters uppercase. Splits input by whitespace to identify words.

public static string ToCamelCase(this string value)

Parameters

value string

The string to convert to camelCase.

Returns

string

A camelCase representation of the input string.

Examples

string result = "Hello World".ToCamelCase(); // "helloWorld"
string result2 = "the quick fox".ToCamelCase(); // "theQuickFox"

ToKebabCase(string)

Returns a lowercase string with hyphens at casing boundary positions. Handles acronyms properly (e.g., "XMLParser" → "xml-parser").

public static string ToKebabCase(this string value)

Parameters

value string

The string to convert to kebab-case.

Returns

string

A kebab-case representation of the input string.

Examples

string result = "HelloWorld".ToKebabCase(); // "hello-world"
string result2 = "XMLParser".ToKebabCase(); // "xml-parser"

ToSlug(string)

Returns a URL-friendly slug: lowercase, only [a-z0-9-], consecutive whitespace collapsed to single hyphen.

public static string ToSlug(this string value)

Parameters

value string

The string to convert to a URL slug.

Returns

string

A URL-friendly slug derived from value.

Examples

string result = "Hello World!".ToSlug(); // "hello-world"
string result2 = "  Multiple   Spaces  ".ToSlug(); // "multiple-spaces"

ToSnakeCase(string)

Returns a lowercase string with underscores at casing boundary positions. Handles acronyms properly (e.g., "XMLParser" → "xml_parser").

public static string ToSnakeCase(this string value)

Parameters

value string

The string to convert to snake_case.

Returns

string

A snake_case representation of the input string.

Examples

string result = "HelloWorld".ToSnakeCase(); // "hello_world"
string result2 = "XMLParser".ToSnakeCase(); // "xml_parser"

ToTitleCaseInvariant(string)

Capitalizes the first letter of each word using invariant culture rules.

public static string ToTitleCaseInvariant(this string value)

Parameters

value string

The string to convert to title case.

Returns

string

A new string with the first letter of each word capitalized and the remaining letters lowercased.

Examples

string result = "hello world".ToTitleCaseInvariant(); // "Hello World"
string result2 = "the QUICK fox".ToTitleCaseInvariant(); // "The Quick Fox"

Truncate(string, int)

Returns the first maxLength characters of the string. If the string length is at most maxLength, returns the string unchanged.

public static string Truncate(this string value, int maxLength)

Parameters

value string

The string to truncate.

maxLength int

The maximum number of characters to return.

Returns

string

A string containing at most maxLength characters from the start of value.

Examples

string result = "Hello, World!".Truncate(5); // "Hello"
string result2 = "Hi".Truncate(10); // "Hi"

Exceptions

ArgumentOutOfRangeException

Thrown when maxLength is negative.

TruncateWithEllipsis(string, int)

Truncates the string and appends an ellipsis ("...") within the maxLength limit. If maxLength is less than 3, truncates without ellipsis.

public static string TruncateWithEllipsis(this string value, int maxLength)

Parameters

value string

The string to truncate.

maxLength int

The maximum total length of the returned string, including the ellipsis.

Returns

string

A string of at most maxLength characters, with trailing "..." if truncation occurred and maxLength is 3 or greater.

Examples

string result = "Hello, World!".TruncateWithEllipsis(8); // "Hello..."
string result2 = "Hi".TruncateWithEllipsis(10); // "Hi"

Exceptions

ArgumentOutOfRangeException

Thrown when maxLength is negative.

TruncateWords(string, int, string)

Returns the first maxWords words followed by the specified suffix. Words are split by whitespace. If the string has fewer or equal words, returns it unchanged.

public static string TruncateWords(this string value, int maxWords, string suffix = "...")

Parameters

value string

The string to truncate by word count.

maxWords int

The maximum number of words to keep.

suffix string

The suffix appended after truncation. Defaults to "...".

Returns

string

A string containing at most maxWords words followed by suffix if truncation occurred; otherwise the original string.

Examples

string result = "The quick brown fox jumps".TruncateWords(3); // "The quick brown..."
string result2 = "Hello".TruncateWords(5); // "Hello"