Table of Contents

Class StringExtensions

Namespace
Plugin.BaseTypeExtensions
Assembly
Plugin.BaseTypeExtensions.dll

Provides extension methods for string manipulation and utility functions.

public static class StringExtensions
Inheritance
StringExtensions
Inherited Members

Methods

After(string?, string)

Extracts the substring after the specified marker.

public static string After(this string? input, string marker)

Parameters

input string

The string to search in.

marker string

The marker to search for.

Returns

string

The substring after the marker, or an empty string if not found or input is null.

Before(string?, string)

Extracts the substring before the specified marker.

public static string Before(this string? input, string marker)

Parameters

input string

The string to search in.

marker string

The marker to search for.

Returns

string

The substring before the marker, or an empty string if not found or input is null.

Between(string?, string, string)

Extracts the substring between two markers.

public static string Between(this string? input, string start, string end)

Parameters

input string

The string to search in.

start string

The start marker.

end string

The end marker.

Returns

string

The substring between the markers, or an empty string if not found or input is null.

CollapseWhitespace(string?)

Collapses multiple consecutive whitespace characters into single spaces.

public static string CollapseWhitespace(this string? input)

Parameters

input string

The string to process.

Returns

string

The string with collapsed whitespace, or an empty string if input is null.

ContainsIgnoreCase(string?, string)

Determines whether the string contains the specified value, ignoring case.

public static bool ContainsIgnoreCase(this string? input, string value)

Parameters

input string

The string to search in.

value string

The value to search for.

Returns

bool

true if the value is found; otherwise, false.

CountOccurrences(string?, char)

Counts the number of occurrences of a character in the string.

public static int CountOccurrences(this string? input, char character)

Parameters

input string

The string to search in.

character char

The character to count.

Returns

int

The number of occurrences, or 0 if input is null.

CountOccurrences(string?, string)

Counts the number of occurrences of a substring in the string.

public static int CountOccurrences(this string? input, string substring)

Parameters

input string

The string to search in.

substring string

The substring to count.

Returns

int

The number of occurrences, or 0 if input or substring is null/empty.

EllipsisIfTooLong(string?, int)

Truncates the string with ellipsis if it exceeds the maximum length.

public static string EllipsisIfTooLong(this string? input, int maxLength)

Parameters

input string

The string to check.

maxLength int

The maximum length before ellipsis.

Returns

string

The string with ellipsis if too long, or an empty string if input is null.

EndsWithIgnoreCase(string?, string)

Determines whether the string ends with the specified value, ignoring case.

public static bool EndsWithIgnoreCase(this string? input, string value)

Parameters

input string

The string to check.

value string

The value to compare.

Returns

bool

true if the string ends with the value; otherwise, false.

EqualsIgnoreCase(string?, string?)

Determines whether the string equals the specified value, ignoring case.

public static bool EqualsIgnoreCase(this string? input, string? value)

Parameters

input string

The string to compare.

value string

The value to compare.

Returns

bool

true if the strings are equal; otherwise, false.

EscapeHtml(string?)

HTML-encodes the string for safe display in HTML.

public static string EscapeHtml(this string? input)

Parameters

input string

The string to encode.

Returns

string

The HTML-encoded string, or an empty string if input is null.

ExtractNumbers(string?)

Extracts all numbers from the string and concatenates them.

public static string ExtractNumbers(this string? input)

Parameters

input string

The string to extract numbers from.

Returns

string

A string containing all extracted numbers, or an empty string if input is null or contains no numbers.

FromBase64(string?, Encoding?)

Converts the Base64-encoded string back to its original form.

public static string FromBase64(this string? input, Encoding? encoding = null)

Parameters

input string

The Base64-encoded string to decode.

encoding Encoding

The encoding to use. Defaults to UTF-8.

Returns

string

The decoded string, or an empty string if input is null or invalid Base64.

IsInteger(string?)

Determines whether the string represents an integer value.

public static bool IsInteger(this string? input)

Parameters

input string

The string to validate.

Returns

bool

true if the string is an integer; otherwise, false.

IsNumeric(string?, IFormatProvider?)

Determines whether the string represents a numeric value.

public static bool IsNumeric(this string? input, IFormatProvider? formatProvider = null)

Parameters

input string

The string to validate.

formatProvider IFormatProvider

Returns

bool

true if the string is numeric; otherwise, false.

IsValidEmail(string?)

Determines whether the string is a valid email address.

public static bool IsValidEmail(this string? input)

Parameters

input string

The string to validate.

Returns

bool

true if the string is a valid email address; otherwise, false.

IsValidUrl(string?)

Determines whether the string is a valid URL.

public static bool IsValidUrl(this string? input)

Parameters

input string

The string to validate.

Returns

bool

true if the string is a valid URL; otherwise, false.

JoinWith(IEnumerable<string>, string)

Joins the string values with the specified separator.

public static string JoinWith(this IEnumerable<string> values, string separator)

Parameters

values IEnumerable<string>

The string values to join.

separator string

The separator to use.

Returns

string

The joined string, or an empty string if values is null or empty.

LineCount(string?)

Counts the number of lines in the string.

public static int LineCount(this string? input)

Parameters

input string

The string to analyze.

Returns

int

The number of lines, or 0 if input is null.

NullIfDud(string?)

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

public static string? NullIfDud(this string? input)

Parameters

input string

The string to check.

Returns

string

null if the string is a "dud" (null/empty/whitespace), otherwise the original string.

OnlyDigits(string?)

Extracts only the digits from the string.

public static string OnlyDigits(this string? input)

Parameters

input string

The string to process.

Returns

string

A string containing only digits, or an empty string if input is null or contains no digits.

OnlyLetters(string?)

Extracts only the letters from the string.

public static string OnlyLetters(this string? input)

Parameters

input string

The string to process.

Returns

string

A string containing only letters, or an empty string if input is null or contains no letters.

PadBoth(string?, int, char)

Pads the string on both sides to center it within the specified total width.

public static string PadBoth(this string? input, int totalWidth, char paddingChar = ' ')

Parameters

input string

The string to pad.

totalWidth int

The total width of the result string.

paddingChar char

The character to use for padding.

Returns

string

The centered string, or an empty string if input is null.

RandomString(int, string)

Generates a random string of the specified length using the allowed characters.

public static string RandomString(int length, string allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")

Parameters

length int

The length of the random string.

allowedChars string

The characters to choose from.

Returns

string

A random string of the specified length.

RemoveDigits(string?)

Removes all digits from the string.

public static string RemoveDigits(this string? input)

Parameters

input string

The string to process.

Returns

string

The string with digits removed, or an empty string if input is null.

RemoveSpecialCharacters(string?)

Removes common special characters from a string, keeping only letters, digits, and spaces.

public static string RemoveSpecialCharacters(this string? input)

Parameters

input string

The string to clean.

Returns

string

A string with special characters removed, or an empty string if input is null.

RemoveWhitespace(string?)

Removes all whitespace characters from the string.

public static string RemoveWhitespace(this string? input)

Parameters

input string

The string to process.

Returns

string

The string with all whitespace removed, or an empty string if input is null.

Repeat(string?, int)

Repeats the string the specified number of times.

public static string Repeat(this string? input, int count)

Parameters

input string

The string to repeat.

count int

The number of times to repeat.

Returns

string

The repeated string, or an empty string if input is null or count is 0 or negative.

Reverse(string?)

Reverses the characters in the string.

public static string Reverse(this string? input)

Parameters

input string

The string to reverse.

Returns

string

The reversed string, or an empty string if input is null.

SplitAndRemoveEmpty(string?, params char[])

Splits the string by the specified separators and removes empty entries.

public static string[] SplitAndRemoveEmpty(this string? input, params char[] separators)

Parameters

input string

The string to split.

separators char[]

The separator characters.

Returns

string[]

An array of non-empty strings, or an empty array if input is null.

SplitAndTrim(string?, params char[])

Splits the string by the specified separators and trims each result.

public static string[] SplitAndTrim(this string? input, params char[] separators)

Parameters

input string

The string to split.

separators char[]

The separator characters.

Returns

string[]

An array of trimmed strings, or an empty array if input is null.

StartsWithIgnoreCase(string?, string)

Determines whether the string starts with the specified value, ignoring case.

public static bool StartsWithIgnoreCase(this string? input, string value)

Parameters

input string

The string to check.

value string

The value to compare.

Returns

bool

true if the string starts with the value; otherwise, false.

ToBase64(string?, Encoding?)

Converts the string to Base64 encoding.

public static string ToBase64(this string? input, Encoding? encoding = null)

Parameters

input string

The string to encode.

encoding Encoding

The encoding to use. Defaults to UTF-8.

Returns

string

The Base64-encoded string, or an empty string if input is null.

ToCamelCase(string?)

Converts the string to camelCase.

public static string ToCamelCase(this string? input)

Parameters

input string

The string to convert.

Returns

string

The string in camelCase, or an empty string if input is null.

ToKebabCase(string?)

Converts the string to kebab-case.

public static string ToKebabCase(this string? input)

Parameters

input string

The string to convert.

Returns

string

The string in kebab-case, or an empty string if input is null.

ToPascalCase(string?)

Converts the string to PascalCase.

public static string ToPascalCase(this string? input)

Parameters

input string

The string to convert.

Returns

string

The string in PascalCase, or an empty string if input is null.

ToSnakeCase(string?)

Converts the string to snake_case.

public static string ToSnakeCase(this string? input)

Parameters

input string

The string to convert.

Returns

string

The string in snake_case, or an empty string if input is null.

ToTitleCase(string?)

Converts the string to title case (first letter of each word capitalized).

public static string ToTitleCase(this string? input)

Parameters

input string

The string to convert.

Returns

string

The string in title case, or an empty string if input is null.

Truncate(string?, int, string)

Truncates the string to the specified maximum length, optionally adding a suffix.

public static string Truncate(this string? input, int maxLength, string suffix = "...")

Parameters

input string

The string to truncate.

maxLength int

The maximum length of the result string.

suffix string

The suffix to append if truncation occurs.

Returns

string

The truncated string, or an empty string if input is null.

UnescapeHtml(string?)

HTML-decodes the string.

public static string UnescapeHtml(this string? input)

Parameters

input string

The string to decode.

Returns

string

The HTML-decoded string, or an empty string if input is null.

WordCount(string?)

Counts the number of words in the string.

public static int WordCount(this string? input)

Parameters

input string

The string to analyze.

Returns

int

The number of words, or 0 if input is null or empty.