-
Notifications
You must be signed in to change notification settings - Fork 1
String Extensions
StringExtensions provides a comprehensive set of extension methods for string values, making common string operations more intuitive and readable.
| 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 |
Determines whether the string is null, empty, or consists only of whitespace.
bool empty = "".IsEmpty(); // true
bool empty2 = " ".IsEmpty(); // true
bool empty3 = "Hello".IsEmpty(); // falseDetermines whether the string is not null, not empty, and not whitespace.
bool notEmpty = "Hello".IsNotEmpty(); // true
bool notEmpty2 = "".IsNotEmpty(); // falseDetermines whether the string represents a valid integer.
bool isInt = "123".IsInteger(); // true
bool isInt2 = "12.3".IsInteger(); // false
bool isInt3 = "abc".IsInteger(); // falseDetermines whether the string represents a valid decimal number.
bool isDecimal = "12.34".IsDecimal(); // true
bool isDecimal2 = "123".IsDecimal(); // true
bool isDecimal3 = "abc".IsDecimal(); // falseDetermines whether the string represents a valid numeric value.
bool isNumeric = "12.34".IsNumeric(); // true
bool isNumeric2 = "123".IsNumeric(); // true
bool isNumeric3 = "abc".IsNumeric(); // falseConverts 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.
Truncates the string to the specified maximum length.
string trimmed = "Hello World".TrimToLength(5); // "Hello"
string trimmed2 = "Hi".TrimToLength(5); // "Hi" (unchanged)Counts the number of occurrences of a character in the string.
int count = "Hello World".CountChar('l'); // 3
int count2 = "Hello World".CountChar('z'); // 0Counts the number of occurrences of a substring in the string.
int count = "Hello Hello Hello".CountSubstring("Hello"); // 3
int count2 = "Hello World".CountSubstring("xyz"); // 0Determines 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"); // falseDetermines 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"); // falseDetermines whether the string contains any of the specified values.
bool contains = "Hello World".ContainsAny("ell", "xyz"); // true
bool contains2 = "Hello World".ContainsAny("abc", "def"); // falseDetermines whether the string contains all of the specified values.
bool containsAll = "Hello World".ContainsAll("Hello", "World"); // true
bool containsAll2 = "Hello World".ContainsAll("Hello", "xyz"); // falseRemoves all whitespace characters from the string.
string noSpace = "Hello World".RemoveWhitespace(); // "HelloWorld"
string noSpace2 = " Hello World ".RemoveWhitespace(); // "HelloWorld"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"]Reverses the string.
string reversed = "Hello".Reverse(); // "olleH"Takes the first n characters from the string.
string first = "Hello World".Take(5); // "Hello"
string first2 = "Hi".Take(5); // "Hi" (unchanged)Takes the last n characters from the string.
string last = "Hello World".Last(5); // "World"
string last2 = "Hi".Last(5); // "Hi" (unchanged)Removes the last n characters from the string.
string removed = "Hello World".RemoveEnd(6); // "Hello"
string removed2 = "Hi".RemoveEnd(5); // "" (empty)Removes the first n characters from the string.
string removed = "Hello World".RemoveStart(6); // "World"
string removed2 = "Hi".RemoveStart(5); // "" (empty)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"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;
}public bool MatchesTags(string tags, params string[] searchTags)
{
return tags.ContainsAny(searchTags);
}
public bool MatchesAllTags(string tags, params string[] searchTags)
{
return tags.ContainsAll(searchTags);
}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);
}public string CleanUserInput(string input)
{
// Remove whitespace, split and trim
string cleaned = input.RemoveWhitespace();
return cleaned;
}public string GetEnumDisplay(Enum value)
{
return value.ToEnumString();
}| 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 |
- MathHelper
- FileHelper
- HashHelper
- JsonHelper
- MapHelper
- TextHelper
- SoundHelper
- AlignHelpers
- Instance Helper
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions