ByteDev.Strings 9.1.0

Library of extended string related functionality.

Install-Package ByteDev.Strings -Version 9.1.0
dotnet add package ByteDev.Strings --version 9.1.0
<PackageReference Include="ByteDev.Strings" Version="9.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ByteDev.Strings --version 9.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ByteDev.Strings, 9.1.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install ByteDev.Strings as a Cake Addin
#addin nuget:?package=ByteDev.Strings&version=9.1.0

// Install ByteDev.Strings as a Cake Tool
#tool nuget:?package=ByteDev.Strings&version=9.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

ByteDev.Strings

Library of extended string related functionality.

Installation

ByteDev.Strings has been written as a .NET Standard 2.0 library, so you can consume it from a .NET Core or .NET Framework 4.6.1 (or greater) application.

ByteDev.Strings is hosted as a package on nuget.org. To install from the Package Manager Console in Visual Studio run:

Install-Package ByteDev.Strings

Further details can be found on the nuget page.

Release Notes

Releases follow semantic versioning.

Full details of the release notes can be viewed on GitHub.

Usage

String Extensions

To use any of the extension method reference the ByteDev.Strings namespace.

String extension methods:

  • ContainsAny
  • ContainsOnly
  • ContainsIgnoreCase
  • CountOccurrences
  • DetectNewLineType
  • EnsureStartsWith
  • EnsureEndsWith
  • FormatWith
  • GetEndNewLine
  • IsDigit
  • IsDigits
  • IsEmpty
  • IsEmailAddress
  • IsFalse
  • IsGuid
  • IsHttpUrl
  • IsIpAddress
  • IsLengthBetween
  • IsLetters
  • IsLowerCase
  • IsNullOrEmpty
  • IsNullOrWhitespace
  • IsNumeric
  • IsPhoneNumber
  • IsUpperCase
  • IsTime
  • IsTrue
  • IsXml
  • Left
  • LeftWithEllipsis
  • LeftWithInnerEllipsis
  • NormalizeNewLinesToUnix
  • NormalizeNewLinesToWindows
  • Pluralize
  • Remove
  • RemoveEndNewLine
  • RemoveStartsWith
  • RemoveEndsWith
  • RemoveBracketedText
  • RemoveWhiteSpace
  • Repeat
  • ReplaceFirst
  • ReplaceLast
  • ReplaceToken
  • Reverse
  • Right
  • SafeLength
  • SafeSubstring
  • ToBool
  • ToBoolOrDefault
  • ToByteArray
  • ToCsv
  • ToDateTime
  • ToDateTimeOrDefault
  • ToEnum
  • ToKeyValuePair
  • ToLines
  • ToInt
  • ToIntOrDefault
  • ToLong
  • ToLongOrDefault
  • ToUri
  • ToTitleCase
  • Wrap

StringBuilder extension methods:

  • AppendIfEmpty
  • AppendIfNotEmpty
  • AppendLines
  • AppendValues
  • IsEmpty

CaseConverter

CaseConverter can be used to change the case of different strings.

Reference namespace: ByteDev.Strings.Case.

CaseConverter has the following methods:

  • ToCamelCase
  • ToKebabCase
  • ToPascalCase
  • ToSnakeCase
  • ToSnakeUpperCase
string s1 = CaseConverter.ToCamelCase("snake_case", CaseType.SnakeCase);   // "snakeCase"

string s2 = CaseConverter.ToPascalCase("kebab-case", CaseType.KebabCase);  // "KebabCase"

bool isPascalCase = s2.IsPascalCase();   // true

There are also a number of case related string extension methods:

  • IsCamelCase
  • IsKebabCase
  • IsPascalCase
  • IsSnakeCase
  • IsSnakeUpperCase
  • IsCaseType

StringCommands

Various StringCommands are included that encapsulate different string operations.

Reference namespace: ByteDev.Strings.StringCommands.

var c1 = new CaseToLowerCommand().SetValue("John Smith");
var c2 = new CopyPasteCommand(0, 4, 0).SetValue("John Smith");

IStringCommandInvoker invoker = new StringCommandInvoker();

invoker.SetCommands(c1, c2);
invoker.Invoke();

// c1.Result == "john smith" 
// c2.Result == "JohnJohn Smith"

Commands can also be chained together using the StringChainedCommand:

// Note: we don't have to call SetValue on each command just on StringChainedCommand
// as this will provide the initial value.

var commands = new List<StringCommand>
{
    new CaseToLowerCommand(),
    new InsertCommand(100, " lives in England."),
    new CutPasteCommand(5, 5, 0)
};

var c1 = new StringChainedCommand(commands).SetValue("John Smith");

IStringCommandInvoker invoker = new StringCommandInvoker();

invoker.SetCommands(c1);
invoker.Invoke();

// command.Result == "smithjohn  lives in England."

ToStringBuilder

The assembly also contains the type ToStringBuilder to help return string representations of an object when overriding it's ToString method.

Reference namespace: ByteDev.Strings.

public class MyClass
{
    public string Name => "John";

    public string Age => null;

    public IEnumerable<string> Address => new List<string>
    {
        "123 Highstreet",
        "London",
        "UK"
    };

    public override string ToString()
    {
        return new ToStringBuilder()
            .WithNullValue("<null>")
            .WithStringQuoteChar('\'')
            .With(nameof(Name), Name)
            .With(nameof(Age), Age)
            .With(nameof(Address), Address)
            .Build();
    }
}

// ...
string s = new MyClass().ToString();

// s == "Name: 'John', Age: <null>, Address: { '123 Highstreet', 'London', 'UK' }"

Masker

Use the Masker type to help mask different types of strings.

Reference namespace: ByteDev.Strings.Masking.

// Initialize Masker type

var options = new MaskerOptions
{
    MaskChar = '#',
    MaskSpace = true
};

var masker = new Masker(options);
// Mask a payment card number

string card = masker.PaymentCard("4111111111111111");

// card == "############1111"
// Mask an email address

string email = masker.EmailAddress("john.smith@gmail.co.uk");

// email = "j#########@#####.co.uk"
// Mask a custom string

string custom = masker.Custom("12345", 1, 2);

// custom = "1##45"

ByteDev.Strings

Library of extended string related functionality.

Installation

ByteDev.Strings has been written as a .NET Standard 2.0 library, so you can consume it from a .NET Core or .NET Framework 4.6.1 (or greater) application.

ByteDev.Strings is hosted as a package on nuget.org. To install from the Package Manager Console in Visual Studio run:

Install-Package ByteDev.Strings

Further details can be found on the nuget page.

Release Notes

Releases follow semantic versioning.

Full details of the release notes can be viewed on GitHub.

Usage

String Extensions

To use any of the extension method reference the ByteDev.Strings namespace.

String extension methods:

  • ContainsAny
  • ContainsOnly
  • ContainsIgnoreCase
  • CountOccurrences
  • DetectNewLineType
  • EnsureStartsWith
  • EnsureEndsWith
  • FormatWith
  • GetEndNewLine
  • IsDigit
  • IsDigits
  • IsEmpty
  • IsEmailAddress
  • IsFalse
  • IsGuid
  • IsHttpUrl
  • IsIpAddress
  • IsLengthBetween
  • IsLetters
  • IsLowerCase
  • IsNullOrEmpty
  • IsNullOrWhitespace
  • IsNumeric
  • IsPhoneNumber
  • IsUpperCase
  • IsTime
  • IsTrue
  • IsXml
  • Left
  • LeftWithEllipsis
  • LeftWithInnerEllipsis
  • NormalizeNewLinesToUnix
  • NormalizeNewLinesToWindows
  • Pluralize
  • Remove
  • RemoveEndNewLine
  • RemoveStartsWith
  • RemoveEndsWith
  • RemoveBracketedText
  • RemoveWhiteSpace
  • Repeat
  • ReplaceFirst
  • ReplaceLast
  • ReplaceToken
  • Reverse
  • Right
  • SafeLength
  • SafeSubstring
  • ToBool
  • ToBoolOrDefault
  • ToByteArray
  • ToCsv
  • ToDateTime
  • ToDateTimeOrDefault
  • ToEnum
  • ToKeyValuePair
  • ToLines
  • ToInt
  • ToIntOrDefault
  • ToLong
  • ToLongOrDefault
  • ToUri
  • ToTitleCase
  • Wrap

StringBuilder extension methods:

  • AppendIfEmpty
  • AppendIfNotEmpty
  • AppendLines
  • AppendValues
  • IsEmpty

CaseConverter

CaseConverter can be used to change the case of different strings.

Reference namespace: ByteDev.Strings.Case.

CaseConverter has the following methods:

  • ToCamelCase
  • ToKebabCase
  • ToPascalCase
  • ToSnakeCase
  • ToSnakeUpperCase
string s1 = CaseConverter.ToCamelCase("snake_case", CaseType.SnakeCase);   // "snakeCase"

string s2 = CaseConverter.ToPascalCase("kebab-case", CaseType.KebabCase);  // "KebabCase"

bool isPascalCase = s2.IsPascalCase();   // true

There are also a number of case related string extension methods:

  • IsCamelCase
  • IsKebabCase
  • IsPascalCase
  • IsSnakeCase
  • IsSnakeUpperCase
  • IsCaseType

StringCommands

Various StringCommands are included that encapsulate different string operations.

Reference namespace: ByteDev.Strings.StringCommands.

var c1 = new CaseToLowerCommand().SetValue("John Smith");
var c2 = new CopyPasteCommand(0, 4, 0).SetValue("John Smith");

IStringCommandInvoker invoker = new StringCommandInvoker();

invoker.SetCommands(c1, c2);
invoker.Invoke();

// c1.Result == "john smith" 
// c2.Result == "JohnJohn Smith"

Commands can also be chained together using the StringChainedCommand:

// Note: we don't have to call SetValue on each command just on StringChainedCommand
// as this will provide the initial value.

var commands = new List<StringCommand>
{
    new CaseToLowerCommand(),
    new InsertCommand(100, " lives in England."),
    new CutPasteCommand(5, 5, 0)
};

var c1 = new StringChainedCommand(commands).SetValue("John Smith");

IStringCommandInvoker invoker = new StringCommandInvoker();

invoker.SetCommands(c1);
invoker.Invoke();

// command.Result == "smithjohn  lives in England."

ToStringBuilder

The assembly also contains the type ToStringBuilder to help return string representations of an object when overriding it's ToString method.

Reference namespace: ByteDev.Strings.

public class MyClass
{
    public string Name => "John";

    public string Age => null;

    public IEnumerable<string> Address => new List<string>
    {
        "123 Highstreet",
        "London",
        "UK"
    };

    public override string ToString()
    {
        return new ToStringBuilder()
            .WithNullValue("<null>")
            .WithStringQuoteChar('\'')
            .With(nameof(Name), Name)
            .With(nameof(Age), Age)
            .With(nameof(Address), Address)
            .Build();
    }
}

// ...
string s = new MyClass().ToString();

// s == "Name: 'John', Age: <null>, Address: { '123 Highstreet', 'London', 'UK' }"

Masker

Use the Masker type to help mask different types of strings.

Reference namespace: ByteDev.Strings.Masking.

// Initialize Masker type

var options = new MaskerOptions
{
    MaskChar = '#',
    MaskSpace = true
};

var masker = new Masker(options);
// Mask a payment card number

string card = masker.PaymentCard("4111111111111111");

// card == "############1111"
// Mask an email address

string email = masker.EmailAddress("john.smith@gmail.co.uk");

// email = "j#########@#####.co.uk"
// Mask a custom string

string custom = masker.Custom("12345", 1, 2);

// custom = "1##45"

  • .NETStandard 2.0

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on ByteDev.Strings:

Package Downloads
ByteDev.Io
Set of IO related .NET utility classes.
ByteDev.Nuget.Nuspec
.NET Standard library for reading nuget nuspec manifest files.
ByteDev.Validation.Fluent
Extensions and functionality to use with the Fluent Validation library.
ByteDev.Subtitles.SubRip
.NET Standard library to help when using SubRip subtitles.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
9.1.0 30 3/29/2021
9.0.0 59 3/15/2021
8.3.0 68 3/10/2021
8.2.0 60 1/28/2021
8.1.0 79 12/31/2020
8.0.0 134 11/10/2020
7.0.0 106 11/5/2020
6.0.0 162 9/30/2020
5.0.0 110 9/28/2020
4.1.0 221 7/17/2020
4.0.0 137 7/1/2020
3.0.0 189 6/16/2020
2.1.0 200 5/29/2020
2.0.1 171 5/29/2020
2.0.0 143 5/28/2020
1.0.0 153 4/30/2020