Skip to content

String Extensions

Shmellyorc edited this page Aug 31, 2026 · 1 revision

StringExtensions provides a comprehensive set of extension methods for string values, making common string operations more intuitive and readable.


Overview

Feature Description
Validation Empty, whitespace, and numeric checks
Enum Conversion Convert enums to strings with caching
Trimming Truncate strings to a maximum length
Counting Count characters and substrings
Pattern Matching Starts/ends/contains with any or all values
Whitespace Remove whitespace from strings
Splitting Split and trim strings
Manipulation Reverse, take, last, remove from ends
Joining Join collections into strings

Validation

IsEmpty

Determines whether the string is null, empty, or consists only of whitespace.

bool empty = "".IsEmpty();        // true
bool empty2 = "   ".IsEmpty();    // true
bool empty3 = "Hello".IsEmpty();  // false

IsNotEmpty

Determines whether the string is not null, not empty, and not whitespace.

bool notEmpty = "Hello".IsNotEmpty();  // true
bool notEmpty2 = "".IsNotEmpty();      // false

Numeric Validation

IsInteger

Determines whether the string represents a valid integer.

bool isInt = "123".IsInteger();    // true
bool isInt2 = "12.3".IsInteger();  // false
bool isInt3 = "abc".IsInteger();   // false

IsDecimal

Determines whether the string represents a valid decimal number.

bool isDecimal = "12.34".IsDecimal();  // true
bool isDecimal2 = "123".IsDecimal();   // true
bool isDecimal3 = "abc".IsDecimal();   // false

IsNumeric

Determines whether the string represents a valid numeric value.

bool isNumeric = "12.34".IsNumeric();  // true
bool isNumeric2 = "123".IsNumeric();   // true
bool isNumeric3 = "abc".IsNumeric();   // false

Enum Conversion

ToEnumString

Converts an enum to its fully qualified string representation with caching.

public enum MyEnum { Value1, Value2, Value3 }

string enumStr = MyEnum.Value1.ToEnumString();
// Returns: "Namespace.MyEnum.Value1"

The result is cached for performance.


Trimming

TrimToLength

Truncates the string to the specified maximum length.

string trimmed = "Hello World".TrimToLength(5);  // "Hello"
string trimmed2 = "Hi".TrimToLength(5);          // "Hi" (unchanged)

Counting

CountChar

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

int count = "Hello World".CountChar('l');  // 3
int count2 = "Hello World".CountChar('z'); // 0

CountSubstring

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

int count = "Hello Hello Hello".CountSubstring("Hello");  // 3
int count2 = "Hello World".CountSubstring("xyz");         // 0

Pattern Matching

StartsWithAny

Determines whether the string starts with any of the specified values.

bool starts = "Hello World".StartsWithAny("He", "Wo");  // true
bool starts2 = "Hello World".StartsWithAny("Xyz", "Abc"); // false

EndsWithAny

Determines whether the string ends with any of the specified values.

bool ends = "Hello World".EndsWithAny("rld", "ld");  // true
bool ends2 = "Hello World".EndsWithAny("xyz", "abc"); // false

ContainsAny

Determines whether the string contains any of the specified values.

bool contains = "Hello World".ContainsAny("ell", "xyz");  // true
bool contains2 = "Hello World".ContainsAny("abc", "def"); // false

ContainsAll

Determines whether the string contains all of the specified values.

bool containsAll = "Hello World".ContainsAll("Hello", "World");  // true
bool containsAll2 = "Hello World".ContainsAll("Hello", "xyz");   // false

Whitespace

RemoveWhitespace

Removes all whitespace characters from the string.

string noSpace = "Hello World".RemoveWhitespace();  // "HelloWorld"
string noSpace2 = "  Hello   World  ".RemoveWhitespace();  // "HelloWorld"

Splitting

SplitAndTrim

Splits the string by a separator, trims each part, and removes empty entries.

string[] parts = "one, two, three".SplitAndTrim(',');  // ["one", "two", "three"]
string[] parts2 = "one,two,three".SplitAndTrim(',');   // ["one", "two", "three"]

Manipulation

Reverse

Reverses the string.

string reversed = "Hello".Reverse();  // "olleH"

Take

Takes the first n characters from the string.

string first = "Hello World".Take(5);  // "Hello"
string first2 = "Hi".Take(5);          // "Hi" (unchanged)

Last

Takes the last n characters from the string.

string last = "Hello World".Last(5);   // "World"
string last2 = "Hi".Last(5);           // "Hi" (unchanged)

RemoveEnd

Removes the last n characters from the string.

string removed = "Hello World".RemoveEnd(6);  // "Hello"
string removed2 = "Hi".RemoveEnd(5);          // "" (empty)

RemoveStart

Removes the first n characters from the string.

string removed = "Hello World".RemoveStart(6);  // "World"
string removed2 = "Hi".RemoveStart(5);          // "" (empty)

Joining

JoinToString

Joins the elements of a collection into a string using the specified separator.

var items = new[] { "a", "b", "c" };
string joined = items.JoinToString(", ");  // "a, b, c"

var numbers = new[] { 1, 2, 3 };
string joined2 = numbers.JoinToString(" - ");  // "1 - 2 - 3"

Examples

Input Validation

public bool ValidateInput(string input)
{
    if (input.IsEmpty())
    {
        Console.WriteLine("Input cannot be empty.");
        return false;
    }
    
    if (!input.IsInteger())
    {
        Console.WriteLine("Input must be a number.");
        return false;
    }
    
    return true;
}

Tag Filtering

public bool MatchesTags(string tags, params string[] searchTags)
{
    return tags.ContainsAny(searchTags);
}

public bool MatchesAllTags(string tags, params string[] searchTags)
{
    return tags.ContainsAll(searchTags);
}

Path Handling

public string GetFileName(string fullPath)
{
    // Remove everything up to the last slash
    string fileName = fullPath.RemoveStart(fullPath.LastIndexOf('/') + 1);
    return fileName;
}

public string GetFileExtension(string fullPath)
{
    // Take everything after the last dot
    return fullPath.Last(fullPath.Length - fullPath.LastIndexOf('.') - 1);
}

Text Cleaning

public string CleanUserInput(string input)
{
    // Remove whitespace, split and trim
    string cleaned = input.RemoveWhitespace();
    return cleaned;
}

Enum Display

public string GetEnumDisplay(Enum value)
{
    return value.ToEnumString();
}

Summary

Method Description
IsEmpty Checks if string is null, empty, or whitespace
IsNotEmpty Checks if string is not empty
IsInteger Checks if string is a valid integer
IsDecimal Checks if string is a valid decimal
IsNumeric Checks if string is a valid numeric
ToEnumString Converts enum to string with caching
TrimToLength Truncates to a maximum length
CountChar Counts character occurrences
CountSubstring Counts substring occurrences
StartsWithAny Checks if starts with any value
EndsWithAny Checks if ends with any value
ContainsAny Checks if contains any value
ContainsAll Checks if contains all values
RemoveWhitespace Removes all whitespace
SplitAndTrim Splits and trims parts
Reverse Reverses the string
Take Takes first n characters
Last Takes last n characters
RemoveEnd Removes last n characters
RemoveStart Removes first n characters
JoinToString Joins collection into a string

Back to Home

Clone this wiki locally