Skip to content

StringExtensions

Lee Conlin edited this page Jul 27, 2021 · 5 revisions

StringExtensions.FromBase64(base64EncodedText, encoding)

Converts Base64 encoded text back into a regular string.

Name Description
base64EncodedText System.String
The Base64 encoded text to decode
encoding System.Text.Encoding
The encoding to use (defaults to UTF8)

Returns

Reverses Base64 encoding, returning the original, unencoded string.

StringExtensions.IsTruthy(text)

Attempts to discern truthiness from a string

Name Description
text System.String

Returns

True if the string is "truthy", otherwise false.

StringExtensions.NthIndexOf(text, ch, n)

Find the Nth index of a character within a string. For example, in the string "a1a2a3a4a5", find the index of the 3rd 'a'.

Name Description
text System.String
The text to search
ch System.Char
The character to search for
n System.Int32
The number of the specified character to count

Returns

The integer index of the nth instance of the specified character within the string.

StringExtensions.RemoveDiacritics(text)

Removed diacritics, replacing them with the visually closest latin character

Name Description
text System.String

Returns

A string with the diacritics replaced with their visually closest matching latin characters. e.g. "Thérèse" becomes "Therese".

StringExtensions.ToBase64(text, encoding)

Converts the plain text to a Base64 encoded string

Name Description
text System.String
The text to encode
encoding System.Text.Encoding
The encoding to use (defaults to UTF8)

Returns

A string containing a Base64 encoded representation of the input string.

StringExtensions.ToDecimal(text)

Converts the textual representation of a decimal to a decimal data type. If not possible, returns 0. Examples: var value = "6.2".ToDecimal(); // decimal value = 6.2m; var value = "$6.2".ToDecimal(); // decimal value = 6.2m; var value = "6".ToDecimal(); // decimal value = 6.0m; var value = "6..".ToDecimal(); // decimal value = 0m; var value = "ab6".ToDecimal(); // decimal value = 0m;

Name Description
text System.String

Returns

The decimal represented by the string, or 0m if the string doesn't represent a decimal (or represents 0m).

StringExtensions.ToInt(text)

Converts the textual representation of an integer to an int data type. If not possible, returns 0. Examples: var value = "6".ToInt(); // int value = 6; var value = "ab6".ToInt(); // int value = 0; var value = "6.2".ToInt(); // int value = 0;

Name Description
text System.String

Returns

The int represented by the string, or 0 if the string doesn't represent an int (or represents 0).

StringExtensions.ToMd5Hash(text, uppercase)

Create an MD5 hash of a string of text.

Name Description
text System.String
Text to hash
uppercase System.Boolean

Returns

The MD5 hash of the input string

StringExtensions.ToSha1Hash(text, uppercase)

Create an SHA1 hash of a string of text.

Name Description
text System.String
Text to hash
uppercase System.Boolean

Returns

The SHA1 hash of the input string

StringExtensions.ToSha256Hash(text, uppercase)

Create an SHA256 hash of a string of text.

Name Description
text System.String
Text to hash
uppercase System.Boolean

Returns

The SHA256 hash of the input string

StringExtensions.ToUrlFriendlyString(text)

Removes all non-alphanumeric characters from a string, replacing them with hyphens. Ensures only one hyphen regardless of the number of consecutive non-alphanumeric characters. Will remove diacritics, replacing them with the visually closest latin character

Name Description
text System.String
The string you want to make URL friendly

Returns

A string that can be used in a URL safely. e.g. "Some aweful ~~~string~~~ with lots of $peci@l ch@racters" becomes "Some-aweful-string-with-lots-of-peci-l-ch-racters"

StringExtensions.Truncate(text, maxLength, style, stringToAppendIfTruncated)

Truncates text based on options selected.

Name Description
text System.String
The text to truncate
maxLength System.Int32
The maximum length (based on style selected) of the text to be returned
style LeeConlin.ExtensionMethods.StringTruncateStyle
MaxCharacters will cut off the string at that number of characters. MaxWords will cut off the string at that number of words (using space as word-break). MaxCharactersAtWordBoundry will attempt to cut the string at a word boundary, keeping the text below the character count requested.
stringToAppendIfTruncated System.String
If provided, will append the given string to the truncated text.

Returns

The truncated string.