StringUtility 1.0.1

dotnet add package StringUtility --version 1.0.1
                    
NuGet\Install-Package StringUtility -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StringUtility" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StringUtility" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="StringUtility" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add StringUtility --version 1.0.1
                    
#r "nuget: StringUtility, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package StringUtility@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=StringUtility&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=StringUtility&version=1.0.1
                    
Install as a Cake Tool

StringUtility Library

A C# library that provides a comprehensive set of extension methods for the string class. This library aims to simplify and enhance string manipulations by offering various commonly used string operations as easy-to-use extension methods.

Installation

You can install the package via NuGet Package Manager:

dotnet add package StringUtility

Features

  • Basic Checks

    • IsNullOrEmpty(): Checks if the string is null or empty.
    • IsNullOrWhiteSpace(): Checks if the string is null or only consists of whitespace characters.
  • String Transformations

    • CapitalizeFirstLetter(): Capitalizes the first letter of the string.
    • ToTitleCase(): Converts the string to title case, where each word starts with a capital letter.
    • ToPascalCase(): Converts a string to PascalCase format.
    • ToKebabCase(): Converts a PascalCase or camelCase string to kebab-case.
    • ToSnakeCase(): Converts a PascalCase or camelCase string to snake_case.
  • String Manipulations

    • Reverse(): Reverses the characters in the string.
    • Repeat(count): Repeats the string count times.
    • Truncate(maxLength): Truncates the string to the specified length.
    • LimitWords(wordLimit): Trims the string to a specified number of words.
    • SafeSubstring(startIndex, length): Safely gets a substring without throwing exceptions.
    • RemoveWhitespace(): Removes all whitespace characters from the string.
    • RemovePunctuation(): Removes all punctuation characters from the string.
    • RemoveDuplicateCharacters(): Removes duplicate characters, keeping only the first occurrence.
    • SanitizeForHtml(): Escapes special characters to make the string safe for HTML.
  • Validation and Checks

    • IsNumeric(): Checks if the string contains only numeric characters.
    • IsValidEmail(): Checks if the string is a valid email address.
    • IsValidUrl(): Checks if the string is a valid URL.
    • IsPalindrome(): Checks if the string is a palindrome, ignoring spaces and casing.
    • ContainsAny(substrings): Checks if the string contains any of the substrings provided in a list.
  • String Analysis

    • WordCount(): Counts the number of words in the string.
    • ToWordList(): Splits the string into a list of words, removing any non-word characters.
    • ExtractNumbers(): Extracts all numbers from the string and returns them as an enumerable of integers.
  • Masking and Hiding Information

    • Mask(visibleCharacters): Masks sensitive information, only showing the last few characters.

Usage

To use the StringUtility library in your C# project:

  1. Import the namespace at the top of your file:

    using StringUtility;
    
  2. Use the extension methods on any string object:

    string example = "hello world";
    
    // Basic Checks
    Console.WriteLine(example.IsNullOrEmpty()); // Output: False
    
    // Transformations
    Console.WriteLine(example.CapitalizeFirstLetter()); // Output: Hello world
    Console.WriteLine("this is a test".ToTitleCase()); // Output: This Is A Test
    
    // Manipulations
    Console.WriteLine(example.Reverse()); // Output: dlrow olleh
    Console.WriteLine("RepeatMe".Repeat(3)); // Output: RepeatMeRepeatMeRepeatMe
    Console.WriteLine("SensitiveInfo1234".Mask()); // Output: ************1234
    
    // Validation
    Console.WriteLine("test@example.com".IsValidEmail()); // Output: True
    Console.WriteLine("https://example.com".IsValidUrl()); // Output: True
    
    // Analysis
    Console.WriteLine("This is a test".WordCount()); // Output: 4
    
    // And many more!
    

Contributing

If you'd like to contribute to this library, feel free to fork the repository and create pull requests. Contributions, issues, and feature requests are welcome!

License

This project is licensed under the MIT License - see the LICENSE file for details.


Enjoy using the StringUtility library to simplify your string manipulation tasks!

Usage Examples

Below are examples demonstrating how to use each of the methods provided by the StringUtility library.

1. Basic Checks

string example = "hello world";
Console.WriteLine(example.IsNullOrEmpty()); // Output: False

string emptyString = "";
Console.WriteLine(emptyString.IsNullOrWhiteSpace()); // Output: True

2. String Transformations

string text = "hello world";
Console.WriteLine(text.CapitalizeFirstLetter()); // Output: Hello world

string title = "this is a test";
Console.WriteLine(title.ToTitleCase()); // Output: This Is A Test

string pascal = "make me pascal case";
Console.WriteLine(pascal.ToPascalCase()); // Output: MakeMePascalCase

string kebab = "ThisIsKebabCase";
Console.WriteLine(kebab.ToKebabCase()); // Output: this-is-kebab-case

string snake = "ThisIsSnakeCase";
Console.WriteLine(snake.ToSnakeCase()); // Output: this_is_snake_case

3. String Manipulations

string reverseExample = "abcdef";
Console.WriteLine(reverseExample.Reverse()); // Output: fedcba

string repeatExample = "RepeatMe";
Console.WriteLine(repeatExample.Repeat(3)); // Output: RepeatMeRepeatMeRepeatMe

string truncateExample = "This is a very long string";
Console.WriteLine(truncateExample.Truncate(10)); // Output: This is a

string limitWordsExample = "This string has too many words to display";
Console.WriteLine(limitWordsExample.LimitWords(5)); // Output: This string has too many

string safeSubstringExample = "Safe substring example";
Console.WriteLine(safeSubstringExample.SafeSubstring(5, 10)); // Output: substring

string whitespaceExample = "Remove    all   whitespace";
Console.WriteLine(whitespaceExample.RemoveWhitespace()); // Output: Removeallwhitespace

string punctuationExample = "Hello, world!";
Console.WriteLine(punctuationExample.RemovePunctuation()); // Output: Hello world

string duplicateExample = "aabbccdd";
Console.WriteLine(duplicateExample.RemoveDuplicateCharacters()); // Output: abcd

string htmlExample = "<div>Hello & welcome!</div>";
Console.WriteLine(htmlExample.SanitizeForHtml()); // Output: &lt;div&gt;Hello &amp; welcome!&lt;/div&gt;

4. Validation and Checks

string numericExample = "12345";
Console.WriteLine(numericExample.IsNumeric()); // Output: True

string emailExample = "test@example.com";
Console.WriteLine(emailExample.IsValidEmail()); // Output: True

string urlExample = "https://example.com";
Console.WriteLine(urlExample.IsValidUrl()); // Output: True

string palindromeExample = "A man a plan a canal Panama";
Console.WriteLine(palindromeExample.IsPalindrome()); // Output: True

string containsExample = "This is a sample text";
Console.WriteLine(containsExample.ContainsAny(new[] { "sample", "text" })); // Output: True

5. String Analysis

string wordCountExample = "Count the words in this string";
Console.WriteLine(wordCountExample.WordCount()); // Output: 6

string wordListExample = "Split this into words";
Console.WriteLine(string.Join(", ", wordListExample.ToWordList())); // Output: Split, this, into, words

string extractNumbersExample = "abc123xyz456";
Console.WriteLine(string.Join(", ", extractNumbersExample.ExtractNumbers())); // Output: 123, 456

6. Masking and Hiding Information

string sensitiveInfo = "SensitiveData12345";
Console.WriteLine(sensitiveInfo.Mask()); // Output: **************12345

string shortSensitiveInfo = "Short";
Console.WriteLine(shortSensitiveInfo.Mask()); // Output: Short (no masking as it's too short)

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Author

Developed by Håvard Brækken.

Acknowledgements

These examples should help you get started with using the StringUtility library in your projects. Enjoy simplifying your string manipulations!

  • Special thanks to the .NET community for providing great resources and inspiration.
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 213 11/3/2024