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
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
Returns
- string
The first
lengthcharacters, or null ifvalueis null.
Exceptions
- ArgumentOutOfRangeException
lengthis 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
valuestringThe string to mask.
visibleCountintThe number of non-whitespace characters to keep visible at the end.
maskCharcharThe character that replaces hidden characters. Defaults to '*'.
Returns
- string
The masked string, or null if
valueis 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
visibleCountis 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
valuestringThe e-mail address.
maskCharcharThe character that replaces hidden characters. Defaults to '*'.
Returns
- string
The masked address, or null if
valueis 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
valuestringThe string to mask.
visibleStartintThe number of non-whitespace characters to keep visible at the start.
visibleEndintThe number of non-whitespace characters to keep visible at the end.
maskCharcharThe character that replaces hidden characters. Defaults to '*'.
Returns
- string
The masked string, or null if
valueis 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
visibleStartorvisibleEndis 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
valuestringThe name.
maskCharcharThe character that replaces hidden characters. Defaults to '*'.
Returns
- string
The masked name, or null if
valueis 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
valuestringThe string.
Returns
- string
The string without diacritics, or an empty string if
valueis 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
valuestringThe source string.
Returns
- string
The string without whitespace, or null if
valueis 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
Returns
- string
The last
lengthcharacters, or null ifvalueis null.
Exceptions
- ArgumentOutOfRangeException
lengthis negative.
ToDashCase(string)
Converts the input string to dash case.
public static string ToDashCase(this string value)
Parameters
valuestringThe 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
valuestringThe 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
valuestringThe string to convert.
cultureCultureInfoThe culture whose casing rules are used, for example tr-TR for İ/ı.
Returns
- string
The title-cased string, or an empty string if
valueis 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
valuestringThe string to shorten.
maxLengthintThe maximum length of the result.
ellipsisstringThe text appended when the string is cut. Defaults to "…".
Returns
- string
The original string if it fits; otherwise the shortened string. Null if
valueis 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
maxLengthis shorter thanellipsis.