✍️String Utility

Summary

Extension methods that are used to check if a string conforms to a pattern or modify the string to fit a pattern.

IsNullOrEmpty

Description

Implements string.IsNullOrEmpty as an extension method

Example

string s = "hello world";

return string.IsNullOrEmpty(s) == s.IsNullOrEmpty(); // true

IsNullOrWhitespace

Description

Implements string.IsNullOrWhitespace as an extension method

Example

string s = "hello world";

return string.IsNullOrWhitespace(s) == s.IsNullOrWhitespace(); // true

ContainsWhitespace

Description

Checks to see if there are any whitespace characters in the string using char.IsWhitespace()

Examples

string s = "hello world";

return s.ContainsWhitespace(); // true

ContainsCharacterFromCollection

Description

Checks to see if any character from the collection is in the source string

Example

string s = "hello world";
var characters = new List<char>() { 'w', 'o', 'r', 'l', 'd' };

return s.ContainsCharacterFromCollection(characters.AsReadOnly()); // true

ContainsOnlyCharactersFromCollection

Description

Checks to see if only characters from the collection are in the source string

Examples

string s = "hello";
var characters = new List<char>() { 'h', 'e', 'l', 'o' };

return s.ContainsOnlyCharactersFromCollection(characters.AsReadOnly()); // true

ContainsUpperCase

Description

Checks to see if any character in the source string is uppercase

Examples

string s = "Hello world";
string s1 = "Hello World";

return s.ContainsUpperCase(); // true
return s1.ContainsUpperCase(); // true

ContainsLowerCase

Description

Checks to see if any character in the source string is lowercase

Examples

string s = "hello world";
string s1 = "Hello World"

return s.ContainsLowerCase(); // true
return s1.ContainsLowerCase(); // true

ContainsMixedCase

Description

Checks to see that the source string contains both uppercase and lowercase characters

Examples

string s = "hello world";

return s.ContainsMixedCase(); // false

ToSnakeCase

Description

This takes any string and will convert it to snake case breaking on _, , and uppercase letters

Examples

"hello" -> "hello"
"helloWorld" -> "hello_world"
"HelloWorld" -> "hello_world"
"hello_world" -> "hello_world"
"hello__world" -> "hello_world"
"hello_world_" -> "hello_world"
"_hello_world" -> "hello_world"
"_hello_world_" -> "hello_world"
"_hello__world" -> "hello_world"
"hello  world" -> "hello_world"
"hello world" -> "hello_world"
"hello_ world" -> "hello_world"
"hello _world" -> "hello_world"
"hello World" -> "hello_world"
"" -> ""

Last updated