Class StringExtensions
Provides extension methods for string manipulation and formatting.
Inherited Members
Namespace: mk.helpers
Assembly: mk.helpers.dll
Syntax
public static class StringExtensions
Methods
BytesToString(long)
Converts a byte count to a human-readable string representation with appropriate unit suffix.
Declaration
public static string BytesToString(this long byteCount)
Parameters
| Type | Name | Description |
|---|---|---|
| long | byteCount | The byte count. |
Returns
| Type | Description |
|---|---|
| string | The human-readable string representation of the byte count. |
ComputeSimilarityLevenshtein(string, string)
Computes the Levenshtein similarity between two strings.
Declaration
public static double ComputeSimilarityLevenshtein(this string source, string target)
Parameters
| Type | Name | Description |
|---|---|---|
| string | source | The source string. |
| string | target | The target string. |
Returns
| Type | Description |
|---|---|
| double | The Levenshtein similarity between the two strings. |
ConvertCapsToWords(string)
Converts camel case or Pascal case string to words with spaces between uppercase letters.
Declaration
public static string ConvertCapsToWords(this string str)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The input string in camel case or Pascal case. |
Returns
| Type | Description |
|---|---|
| string | The input string with spaces added between uppercase letters. |
GetDaySuffix(int)
Gets the appropriate suffix for a day based on its numeric value.
Declaration
public static string GetDaySuffix(this int day)
Parameters
| Type | Name | Description |
|---|---|---|
| int | day | The day value. |
Returns
| Type | Description |
|---|---|
| string | The day suffix (st, nd, rd, or th). |
IsNullOrEmpty(string)
Determines whether the specified string is null or empty.
Declaration
public static bool IsNullOrEmpty(this string str)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The input string. |
Returns
| Type | Description |
|---|---|
| bool | true if the string is null or empty; otherwise, false. |
IsNullOrWhiteSpace(string)
Determines whether the specified string is null, empty, or consists only of white-space characters.
Declaration
public static bool IsNullOrWhiteSpace(this string str)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The input string. |
Returns
| Type | Description |
|---|---|
| bool | true if the string is null, empty, or consists only of white-space characters; otherwise, false. |
KiloFormat(decimal)
Formats an integer as a string with Kilo notation (e.g., 1.5K).
Declaration
public static string KiloFormat(this decimal num)
Parameters
| Type | Name | Description |
|---|---|---|
| decimal | num | The input integer. |
Returns
| Type | Description |
|---|---|
| string | A string with Kilo notation. |
KiloFormat(int)
Formats an integer as a string with Kilo notation (e.g., 1.5K).
Declaration
public static string KiloFormat(this int num)
Parameters
| Type | Name | Description |
|---|---|---|
| int | num | The input integer. |
Returns
| Type | Description |
|---|---|
| string | A string with Kilo notation. |
NormalizePostcode(string)
Normalizes a postcode by converting it to uppercase and adding spaces where appropriate.
Declaration
public static string NormalizePostcode(this string postcode)
Parameters
| Type | Name | Description |
|---|---|---|
| string | postcode | The input postcode string. |
Returns
| Type | Description |
|---|---|
| string | The normalized version of the input postcode. |
RemoveAccent(string)
Removes accents from a string using Cyrillic encoding and converts to ASCII encoding.
Declaration
public static string RemoveAccent(this string txt)
Parameters
| Type | Name | Description |
|---|---|---|
| string | txt | The input string. |
Returns
| Type | Description |
|---|---|
| string | The string with accents removed and converted to ASCII encoding. |
ReplaceStart(string, string, string)
Replaces a specified substring at the start of the target string with a replacement string.
Declaration
public static string ReplaceStart(this string str, string start, string replacement)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The target string. |
| string | start | The substring to be replaced at the start of the target. |
| string | replacement | The replacement string. |
Returns
| Type | Description |
|---|---|
| string | The modified string after replacing the specified substring at the start. |
ToBase64(string, Encoding)
Short hand method to convert a string to base64 encoded string
Declaration
public static string ToBase64(this string str, Encoding encoding = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | Input string |
| Encoding | encoding | The type of text encoding to use. Defaults to Encoding.UTF8 |
Returns
| Type | Description |
|---|---|
| string |
ToBase64Bytes(string, Encoding)
Short hand method to convert a string to base64 encoded bytes
Declaration
public static byte[] ToBase64Bytes(this string str, Encoding encoding = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | Input string |
| Encoding | encoding | The type of text encoding to use. Defaults to Encoding.UTF8 |
Returns
| Type | Description |
|---|---|
| byte[] |
ToBytesAscii(string)
Converts a string to a byte array using ASCII encoding.
Declaration
public static byte[] ToBytesAscii(this string str)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The input string. |
Returns
| Type | Description |
|---|---|
| byte[] | The byte array representation of the input string using ASCII encoding. |
ToBytesUnicode(string)
Converts a string to a byte array using Unicode encoding (UTF-16).
Declaration
public static byte[] ToBytesUnicode(this string str)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The input string. |
Returns
| Type | Description |
|---|---|
| byte[] | The byte array representation of the input string using Unicode encoding (UTF-16). |
ToBytesUtf32(string)
Converts a string to a byte array using UTF-32 encoding.
Declaration
public static byte[] ToBytesUtf32(this string str)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The input string. |
Returns
| Type | Description |
|---|---|
| byte[] | The byte array representation of the input string using UTF-32 encoding. |
ToBytesUtf7(string)
Converts a string to a byte array using UTF-7 encoding.
Declaration
public static byte[] ToBytesUtf7(this string str)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The input string. |
Returns
| Type | Description |
|---|---|
| byte[] | The byte array representation of the input string using UTF-7 encoding. |
ToBytesUtf8(string)
Converts a string to a byte array using UTF-8 encoding.
Declaration
public static byte[] ToBytesUtf8(this string str)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The input string. |
Returns
| Type | Description |
|---|---|
| byte[] | The byte array representation of the input string using UTF-8 encoding. |
ToHtml(string)
Converts a string to its HTML representation with line breaks and non-breaking spaces.
Declaration
public static string ToHtml(this string text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The input string. |
Returns
| Type | Description |
|---|---|
| string | The HTML-encoded string with line breaks and non-breaking spaces. |
ToKMB(decimal)
Converts a decimal number to a formatted string with K, M, or B suffix.
Declaration
public static string ToKMB(this decimal num)
Parameters
| Type | Name | Description |
|---|---|---|
| decimal | num | The input decimal number. |
Returns
| Type | Description |
|---|---|
| string | A formatted string with K, M, or B suffix. |
ToKMB(string)
Converts a string representation of a number to a formatted string with K, M, or B suffix.
Declaration
public static string ToKMB(this string data)
Parameters
| Type | Name | Description |
|---|---|---|
| string | data | The input string containing the number. |
Returns
| Type | Description |
|---|---|
| string | A formatted string with K, M, or B suffix. |
ToSlug(string)
Converts a string to a URL-friendly slug.
Declaration
public static string ToSlug(this string phrase)
Parameters
| Type | Name | Description |
|---|---|---|
| string | phrase | The input string. |
Returns
| Type | Description |
|---|---|
| string | The URL-friendly slug version of the input string. |
ToTitleCase(string)
Converts a string to title case using the current thread's culture.
Declaration
public static string ToTitleCase(this string str)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The input string. |
Returns
| Type | Description |
|---|---|
| string | The input string converted to title case. |
ToTitleCase(string, CultureInfo)
Converts a string to title case using the specified culture.
Declaration
public static string ToTitleCase(this string str, CultureInfo cultureInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The input string. |
| CultureInfo | cultureInfo | The culture to use for title case conversion. |
Returns
| Type | Description |
|---|---|
| string | The input string converted to title case using the specified culture. |
ToTitleCase(string, string)
Converts a string to title case using the specified culture.
Declaration
public static string ToTitleCase(this string str, string cultureInfoName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The input string. |
| string | cultureInfoName | The name of the culture to use. |
Returns
| Type | Description |
|---|---|
| string | The input string converted to title case using the specified culture. |
ToUpperCase(string)
Converts a string to uppercase.
Declaration
public static string ToUpperCase(this string str)
Parameters
| Type | Name | Description |
|---|---|---|
| string | str | The input string. |
Returns
| Type | Description |
|---|---|
| string | The input string converted to uppercase. |
TrimEnd(string, string)
Removes a specified substring from the end of the target string.
Declaration
public static string TrimEnd(this string target, string trimString)
Parameters
| Type | Name | Description |
|---|---|---|
| string | target | The target string. |
| string | trimString | The substring to be removed from the end. |
Returns
| Type | Description |
|---|---|
| string | The modified string after trimming the specified substring from the end. |
TrimStart(string, string)
Removes a specified substring from the start of the target string.
Declaration
public static string TrimStart(this string target, string trimString)
Parameters
| Type | Name | Description |
|---|---|---|
| string | target | The target string. |
| string | trimString | The substring to be removed from the start. |
Returns
| Type | Description |
|---|---|
| string | The modified string after trimming the specified substring from the start. |