Table of Contents

Class StringManipulationExtensions

Namespace
HeroKit.Strings
Assembly
HeroKit.dll

Provides extension methods for string manipulations.

public static class StringManipulationExtensions
Inheritance
StringManipulationExtensions
Inherited Members

Methods

Format(string, params object[])

Replaces the format items in a specified string with the string representation of specified objects.

public static string Format(this string value, params object[] args)

Parameters

value string

A composite format string.

args object[]

An array of objects to format.

Returns

string

A copy of value in which the format items have been replaced by the string representations of the corresponding objects in args.

Left(string?, int)

Returns the first characters of a string. Shorter strings are returned whole.

public static string? Left(this string? value, int length)

Parameters

value string

The source string.

length int

The maximum number of characters to return.

Returns

string

The first length characters, or null if value is null.

Exceptions

ArgumentOutOfRangeException

length is negative.

Mask(string?, int, char)

Hides all but the last characters of a string, for example a card number, phone number or IBAN.

public static string? Mask(this string? value, int visibleCount = 4, char maskChar = '*')

Parameters

value string

The string to mask.

visibleCount int

The number of non-whitespace characters to keep visible at the end.

maskChar char

The character that replaces hidden characters. Defaults to '*'.

Returns

string

The masked string, or null if value is null.

Examples

"5321234567".Mask();        // "******4567"
"0532 123 45 67".Mask(2);   // "**** *** ** 67"

Remarks

Whitespace is kept and not counted, so formatted values keep their layout: "TR33 0006 1005" becomes "**** **** 1005".

Exceptions

ArgumentOutOfRangeException

visibleCount is negative.

MaskEmail(string?, char)

Hides an e-mail address except the first character of the local part and the domain.

public static string? MaskEmail(this string? value, char maskChar = '*')

Parameters

value string

The e-mail address.

maskChar char

The character that replaces hidden characters. Defaults to '*'.

Returns

string

The masked address, or null if value is null. A value without a local part and a domain is hidden completely.

Examples

"emre.akdemir@example.com".MaskEmail(); // "e***********@example.com"

MaskMiddle(string?, int, int, char)

Hides the middle of a string and keeps characters visible at both ends, for example a national id, IBAN or phone number in logs.

public static string? MaskMiddle(this string? value, int visibleStart, int visibleEnd, char maskChar = '*')

Parameters

value string

The string to mask.

visibleStart int

The number of non-whitespace characters to keep visible at the start.

visibleEnd int

The number of non-whitespace characters to keep visible at the end.

maskChar char

The character that replaces hidden characters. Defaults to '*'.

Returns

string

The masked string, or null if value is null.

Examples

"10000000146".MaskMiddle(2, 2);                      // "10*******46"
"TR33 0006 1005 1978 6457 8413 26".MaskMiddle(4, 4); // "TR33 **** **** **** **** **13 26"
"0532 123 45 67".MaskMiddle(4, 2);                   // "0532 *** ** 67"

Remarks

Whitespace is kept and not counted, so formatted values keep their layout. When the string has no more non-whitespace characters than visibleStart and visibleEnd together, every character is hidden, so a short value is never shown whole.

Exceptions

ArgumentOutOfRangeException

visibleStart or visibleEnd is negative.

MaskName(string?, char)

Hides a personal name except the first letter of each word.

public static string? MaskName(this string? value, char maskChar = '*')

Parameters

value string

The name.

maskChar char

The character that replaces hidden characters. Defaults to '*'.

Returns

string

The masked name, or null if value is null.

Examples

"Ayşe Nur Yılmaz".MaskName(); // "A*** N** Y*****"

RemoveDiacritics(string?)

Removes diacritics from Latin letters: "Çağdaş Şehir" becomes "Cagdas Sehir", "Ilıca" becomes "Ilica", "Łódź" becomes "Lodz".

public static string RemoveDiacritics(this string? value)

Parameters

value string

The string.

Returns

string

The string without diacritics, or an empty string if value is null.

Remarks

Uses a fixed character table instead of Unicode normalization, so the result does not depend on ICU and is the same with invariant globalization. Covers Turkish, Latin-1 and common Central European letters; other characters are kept.

RemoveWhitespace(string?)

Removes all whitespace characters, including tabs, line breaks and non-breaking spaces.

public static string? RemoveWhitespace(this string? value)

Parameters

value string

The source string.

Returns

string

The string without whitespace, or null if value is null.

Examples

"TR33 0006\t1005".RemoveWhitespace(); // "TR3300061005"

Right(string?, int)

Returns the last characters of a string. Shorter strings are returned whole.

public static string? Right(this string? value, int length)

Parameters

value string

The source string.

length int

The maximum number of characters to return.

Returns

string

The last length characters, or null if value is null.

Exceptions

ArgumentOutOfRangeException

length is negative.

ToDashCase(string)

Converts the input string to dash case.

public static string ToDashCase(this string value)

Parameters

value string

The input string.

Returns

string

The string converted to dash case.

ToSlug(string?)

Converts a string into a URL-friendly slug.

public static string ToSlug(this string? value)

Parameters

value string

The string to convert.

Returns

string

The URL-friendly slug.

ToTitleCase(string?, CultureInfo)

Converts a string to title case with the casing rules of the given culture.

public static string ToTitleCase(this string? value, CultureInfo culture)

Parameters

value string

The string to convert.

culture CultureInfo

The culture whose casing rules are used, for example tr-TR for İ/ı.

Returns

string

The title-cased string, or an empty string if value is null.

Examples

"istanbul ılıca".ToTitleCase(CultureInfo.GetCultureInfo("tr-TR")); // "İstanbul Ilıca"
"HELLO WORLD".ToTitleCase(CultureInfo.InvariantCulture);          // "Hello World"

Remarks

Words start after whitespace or a hyphen. The first letter of each word is upper-cased and the rest is lower-cased, so all-caps input is converted too (unlike ToTitleCase(string)). Letters after an apostrophe stay lower case: "istanbul'da" becomes "İstanbul'da" with tr-TR.

Truncate(string?, int, string)

Shortens a string to a maximum length and appends an ellipsis when it was cut.

public static string? Truncate(this string? value, int maxLength, string ellipsis = "…")

Parameters

value string

The string to shorten.

maxLength int

The maximum length of the result.

ellipsis string

The text appended when the string is cut. Defaults to "…".

Returns

string

The original string if it fits; otherwise the shortened string. Null if value is null.

Examples

"HeroKit extensions".Truncate(10);      // "HeroKit e…"
"HeroKit".Truncate(10);                 // "HeroKit"

Remarks

The result, including the ellipsis, is never longer than maxLength. The cut never splits a surrogate pair, so emoji and other characters outside the BMP stay intact.

Exceptions

ArgumentOutOfRangeException

maxLength is shorter than ellipsis.