Library of extended string related functionality.
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.
Releases follow semantic versioning.
Full details of the release notes can be viewed on GitHub.
To use any of the extension method reference the ByteDev.Strings namespace.
String extension methods:
- ContainsAll
- ContainsAny
- ContainsOnly
- ContainsIgnoreCase
- ContainsWhiteSpace
- CountOccurrences
- DetectNewLineType
- EnsureStartsWith
- EnsureEndsWith
- FormatWith
- GetEndNewLine
- InsertBeforeUpperCase
- IsAscii
- IsDateTime
- IsDigit
- IsDigits
- IsEmpty
- IsEmailAddress
- IsFalse
- IsGuid
- IsHttpUrl
- IsIpAddress
- IsLengthBetween
- IsLetters
- IsLowerCase
- IsNullOrEmpty
- IsNullOrWhitespace
- IsNumeric
- IsPhoneNumber
- IsUpperCase
- IsUri
- IsTime
- IsTrue
- IsXml
- Left
- LeftWithEllipsis
- LeftWithInnerEllipsis
- NormalizeNewLinesToUnix
- NormalizeNewLinesToWindows
- Pluralize
- Remove
- RemoveBracketedText
- RemoveEndNewLine
- RemoveEndsWith
- RemoveLeadingZeros
- ReplaceMultiOccurrences
- RemoveNonDigits
- RemoveStartsWith
- RemoveWhiteSpace
- Repeat
- ReplaceFirst
- ReplaceLast
- ReplaceToken
- Reverse
- Right
- SafeGetChar
- SafeLength
- SafeSubstring
- ToBool
- ToBoolExtended
- ToBoolOrDefault
- ToByteArray
- ToSequence
- ToEnum
- ToGuid
- ToInt32
- ToInt32OrDefault
- ToInt64
- ToInt64OrDefault
- ToKeyValuePair
- ToLines
- ToLinesList
- ToMemoryStream
- ToUri
- ToTitleCase
- Wrap
StringBuilder extension methods:
- AppendIfEmpty
- AppendIfNotEmpty
- AppendLineIfEmpty
- AppendLineIfNotEmpty
- AppendLines
- AppendValues
- IsEmpty
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
strings1=CaseConverter.ToCamelCase("snake_case",CaseType.SnakeCase);// "snakeCase"strings2=CaseConverter.ToPascalCase("kebab-case",CaseType.KebabCase);// "KebabCase"boolisPascalCase=s2.IsPascalCase();// trueThere are also a number of case related string extension methods:
- IsCamelCase
- IsKebabCase
- IsPascalCase
- IsSnakeCase
- IsSnakeUpperCase
- IsCaseType
Various StringCommands are included that encapsulate different string operations.
Reference namespace: ByteDev.Strings.StringCommands.
varc1=newCaseToLowerCommand().SetValue("John Smith");varc2=newCopyPasteCommand(0,4,0).SetValue("John Smith");IStringCommandInvokerinvoker=newStringCommandInvoker();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.varcommands=newList<StringCommand>{newCaseToLowerCommand(),newInsertCommand(100," lives in England."),newCutPasteCommand(5,5,0)};varc1=newStringChainedCommand(commands).SetValue("John Smith");IStringCommandInvokerinvoker=newStringCommandInvoker();invoker.SetCommands(c1);invoker.Invoke();// command.Result == "smithjohn lives in England."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.
publicclassMyClass{publicstringName=>"John";publicstringAge=>null;publicIEnumerable<string>Address=>newList<string>{"123 Highstreet","London","UK"};publicoverridestringToString(){returnnewToStringBuilder().WithNullValue("<null>").WithStringQuoteChar('\'').With(nameof(Name),Name).With(nameof(Age),Age).With(nameof(Address),Address).Build();}}// ...strings=newMyClass().ToString();// s == "Name: 'John', Age: <null>, Address: { '123 Highstreet', 'London', 'UK' }"Use the Masker type to help mask different types of strings.
Reference namespace: ByteDev.Strings.Masking.
// Initialize Masker typevaroptions=newMaskerOptions{MaskChar='#',MaskSpace=true};varmasker=newMasker(options);// Mask a payment card numberstringcard=masker.PaymentCard("4111111111111111");// card == "############1111"// Mask an email addressstringemail=masker.EmailAddress("john.smith@gmail.co.uk");// email == "j#########@#####.co.uk"// Mask a custom stringstringcustom=masker.Custom("12345",1,2);// custom == "1##45"