Skip to content

TJC-Tools/TJC.StringExtensions

Repository files navigation

NuGet Version and Downloads count

Table of Contents


Pluralize

string string.Pluralize(int number, string? pluralized = null)

  • Takes a string and pluralizes it based on the number provided.
var item = "apple";
var plural = item.Pluralize(2);
Console.WriteLine(plural); // apples

string IEnumerable<object>.Pluralize(string nonPluralized, string? pluralized = null)

  • Takes a collection of objects and pluralizes a string based on the number of objects in the collection.
// Multiple items in a collection results in a pluralized string
var itemsMultiple = new List<string> { "item1", "item2", "item3" };
var plural = itemsMultiple.Pluralize("item");
Console.WriteLine(plural); // items

// A single item in a collection results in a singular string
var itemsSingle = new List<string> { "item1" };
var singluar = itemsSingle.Pluralize("item");
Console.WriteLine(singluar); // item

Separator

  • Joins a collection of strings with a comma and space.
var items = new List<string> { "one", "two", "three" };
var result = items.JoinComma(); // one, two, three

Parsing

  • Keep letters only.
// Arrange
var input = "a1!b2@c3#.d4$ e5%";
var result = input.KeepAlpha(); // abcde
  • Keep numbers only.
// Arrange
var input = "a1!b2@c3#.d4$ e5%";
var result = input.KeepNumeric(); // 12345
  • Keep numbers and periods only.
// Arrange
var input = "a1!b2@c3#.d4$ e5%";
var result = input.KeepNumericAndPeriod(); // 123.45
  • Keep letters and numbers only.
// Arrange
var input = "a1!b2@c3#.d4$ e5%";
var result = input.KeepAlphaNumeric(); // a1b2c3d4e5
  • Keep letters, numbers, and spaces only.
// Arrange
var input = "a1!b2@c3#.d4$ e5%";
var result = input.KeepAlphaNumericAndSpace(); // a1b2c3d4 e5

string string.RemoveSymbols(char[]? exceptions = null)

  • Remove symbols from a string, except any exceptions provided.
// Arrange
var input = "a1!b2@c3#.d4$ e5%";
var result = input.RemoveSymbols(['!', '.']); // a1!b2c3.d4 e5

Lines

  • Splits a string into lines on a new line character.

IEnumerable<string> string.SplitLines(int width = 60, char separator = ' ')

  • Splits a string into lines of a max width with a separator character.
  • Remove multiple blank lines from a string.

Padding

  • Pads both side of a string so that it's centered between the padding