Legend

Class
Struct
Enum
Interface
Delegate
Constructor
Method
Property
Event
Field

Static class: RT.Util.ExtensionMethods.StringExtensions

Summary

Provides extension methods on the String type.

Static methods

string
AddLine(this string str, string line)
Adds the specified line to the end of the current string. Returns the line if the current string is null.
void
AppendJsEscaped(this StringBuilder sb, string input, JsQuotes quotes = JsQuotes.Double)
Appends a JavaScript- or JSON-compatible representation of the string with the appropriate characters escaped into the specified StringBuilder.
byte[]
Base64UrlDecode(this string input)
Decodes this string from base-64-url encoding, which is safe for use in URLs and does not contain the unnecessary padding when the number of bytes is not divisible by 3, into a byte array.
string
Base64UrlEncode(this byte[] bytes)
Encodes this byte array to base-64-url format, which is safe for use in URLs and does not contain the unnecessary padding when the number of bytes is not divisible by 3.
string
CLiteralEscape(this string value)
Escapes all characters in this string whose code is less than 32 or form invalid UTF-16 using C/C#-compatible backslash escapes.
string
CLiteralUnescape(this string value)
Reverses the escaping done by StringExtensions.CLiteralEscape(this string). Note that unescaping is not fully C/C#-compatible in the sense that not all strings that are valid string literals in C/C# can be correctly unescaped by this procedure.
bool
ContainsIgnoreCase(this string strthis, string str)
Determines whether this string contains the other string using the ordinal case-insensitive comparison (StringComparison.OrdinalIgnoreCase).
bool
ContainsNoCase(this string strthis, string str)
Use StringExtensions.ContainsIgnoreCase(this string, string).
bool
EndsWith(this string str, char? ch)
Returns true if and only if this string ends with the specified character.
bool
EndsWithIgnoreCase(this string haystack, string value)
Determines whether the end of this string instance matches the specified string using the ordinal case-insensitive comparison (StringComparison.OrdinalIgnoreCase).
bool
EqualsIgnoreCase(this string strthis, string str)
Determines whether this string is equal to the other string using the ordinal case-insensitive comparison (StringComparison.OrdinalIgnoreCase).
bool
EqualsNoCase(this string strthis, string str)
Use StringExtensions.EqualsIgnoreCase(this string, string).
string
FilenameCharactersEscape(this string input, bool includeNonAscii = false)
Escapes all characters in this string which cannot form part of a valid filename on at least one supported filesystem. The escaping is fully reversible (via StringExtensions.FilenameCharactersUnescape(this string)), but does not treat characters at specific positions differently (e.g. the "." at the end of the name is not escaped, even though it will disappear on a Win32 system).
string
FilenameCharactersUnescape(this string input)
Reverses the transformation done by StringExtensions.FilenameCharactersEscape(this string, bool). This routine will also work on filenames that cannot have been generated by the above escape procedure; any "invalid" escapes will be preserved as-is.
byte[]
FromHex(this string input)
Reconstructs a byte array from its hexadecimal representation (“hexdump”).
string
FromUtf16(this byte[] input)
Converts the specified raw UTF-16 (little-endian) data to a string.
string
FromUtf16BE(this byte[] input)
Converts the specified raw UTF-16 (big-endian) data to a string.
string
FromUtf8(this byte[] input, bool removeBom = false)
Converts the specified raw UTF-8 data to a string.
string
HtmlEscape(this string input, bool leaveSingleQuotesAlone = false, bool leaveDoubleQuotesAlone = false)
Escapes all necessary characters in the specified string so as to make it usable safely in an HTML or XML context.
string
Indent(this string str, int by, bool indentFirstLine = true)
Inserts spaces at the beginning of every line contained within the specified string.
bool
IsNumeric(this string str)
Determines whether the string contains only the characters 0-9.
string
JsEscape(this string input, JsQuotes quotes = JsQuotes.Double)
Returns a JavaScript- or JSON-compatible representation of the string with the appropriate characters escaped.
string
JsEscapeNull(this string input, JsQuotes quotes = JsQuotes.Double)
Returns a JavaScript- or JSON-compatible representation of the string with the appropriate characters escaped. Returns "null" if the input is null.
decimal
ParseDecimal(this string str, NumberStyles style = AllowThousands, Float, IFormatProvider provider = null)
Parses this string as a decimal using decimal.Parse(string).
decimal
ParseDecimal(this string str, decimal default_, NumberStyles style = AllowThousands, Float, IFormatProvider provider = null)
Parses this string as a decimal using decimal.TryParse(string, out decimal). Returns default_ if the value cannot be parsed.
decimal?
ParseDecimalOrNull(this string str, NumberStyles style = AllowThousands, Float, IFormatProvider provider = null)
Parses this string as a decimal using decimal.TryParse(string, out decimal). Returns null if the value cannot be parsed.
double
ParseDouble(this string str, NumberStyles style = AllowThousands, Float, IFormatProvider provider = null)
Parses this string as a double using double.Parse(string).
double
ParseDouble(this string str, double default_, NumberStyles style = AllowThousands, Float, IFormatProvider provider = null)
Parses this string as a double using double.TryParse(string, out double). Returns default_ if the value cannot be parsed.
double?
ParseDoubleOrNull(this string str, NumberStyles style = AllowThousands, Float, IFormatProvider provider = null)
Parses this string as a double using double.TryParse(string, out double). Returns null if the value cannot be parsed.
float
ParseFloat(this string str, NumberStyles style = AllowThousands, Float, IFormatProvider provider = null)
Parses this string as a float using float.Parse(string).
float
ParseFloat(this string str, float default_, NumberStyles style = AllowThousands, Float, IFormatProvider provider = null)
Parses this string as a float using float.TryParse(string, out float). Returns default_ if the value cannot be parsed.
float?
ParseFloatOrNull(this string str, NumberStyles style = AllowThousands, Float, IFormatProvider provider = null)
Parses this string as a float using float.TryParse(string, out float). Returns null if the value cannot be parsed.
int
ParseInt(this string str, NumberStyles style = Integer, IFormatProvider provider = null)
Parses this string as an int using int.Parse(string).
int
ParseInt(this string str, int default_, NumberStyles style = Integer, IFormatProvider provider = null)
Parses this string as an int using int.TryParse(string, out int). Returns default_ if the value cannot be parsed.
int?
ParseIntOrNull(this string str, NumberStyles style = Integer, IFormatProvider provider = null)
Parses this string as an int using int.TryParse(string, out int). Returns null if the value cannot be parsed.
long
ParseLong(this string str, NumberStyles style = Integer, IFormatProvider provider = null)
Parses this string as a long using long.Parse(string).
long
ParseLong(this string str, long default_, NumberStyles style = Integer, IFormatProvider provider = null)
Parses this string as a long using long.TryParse(string, out long). Returns default_ if the value cannot be parsed.
long?
ParseLongOrNull(this string str, NumberStyles style = Integer, IFormatProvider provider = null)
Parses this string as a long using long.TryParse(string, out long). Returns null if the value cannot be parsed.
uint
ParseUInt(this string str, NumberStyles style = Integer, IFormatProvider provider = null)
Parses this string as a uint using uint.Parse(string).
uint
ParseUInt(this string str, uint default_, NumberStyles style = Integer, IFormatProvider provider = null)
Parses this string as a uint using uint.TryParse(string, out uint). Returns default_ if the value cannot be parsed.
uint?
ParseUIntOrNull(this string str, NumberStyles style = Integer, IFormatProvider provider = null)
Parses this string as a uint using uint.TryParse(string, out uint). Returns null if the value cannot be parsed.
ulong
ParseULong(this string str, NumberStyles style = Integer, IFormatProvider provider = null)
Parses this string as a ulong using ulong.Parse(string).
ulong
ParseULong(this string str, ulong default_, NumberStyles style = Integer, IFormatProvider provider = null)
Parses this string as a ulong using ulong.TryParse(string, out ulong). Returns default_ if the value cannot be parsed.
ulong?
ParseULongOrNull(this string str, NumberStyles style = Integer, IFormatProvider provider = null)
Parses this string as a ulong using ulong.TryParse(string, out ulong). Returns null if the value cannot be parsed.
bool
RegexMatch(this string input, string pattern, out Match match)
Determines whether the input string matches the specified regular expression pattern.
bool
RegexMatch(this string input, string pattern, RegexOptions options, out Match match)
MatchCollection
RegexMatches(this string input, string pattern, RegexOptions options = None)
Searches the specified input string for all occurrences of a specified regular expression pattern.
MatchCollection
RegexMatchesIgnoreCase(this string input, string pattern)
bool
RegexMatchIgnoreCase(this string input, string pattern, out Match match)
Determines whether the input string matches the specified regular expression pattern using a culture-invariant case-insensitive match.
string
RegexReplace(this string input, string pattern, string replacement, RegexOptions options = None)
In a specified input string, replaces all strings that match a specified regular expression pattern with a specified replacement string.
string
RegexReplace(this string input, string pattern, MatchEvaluator evaluator, RegexOptions options = None)
In a specified input string, replaces all strings that match a specified regular expression pattern with a string returned by a match evaluator delegate.
string
RegexReplaceIgnoreCase(this string input, string pattern, string replacement)
In a specified input string, replaces all strings that match a specified regular expression pattern with a specified replacement string. The pattern is matched using an invariant-culture case-insensitive match.
string
RegexReplaceIgnoreCase(this string input, string pattern, MatchEvaluator evaluator)
In a specified input string, replaces all strings that match a specified regular expression pattern with a string returned by a match evaluator delegate. The pattern is matched using an invariant-culture case-insensitive match.
string[]
RegexSplit(this string input, string pattern, RegexOptions options = None)
Splits the input string at the position defined by pattern.
string[]
RegexSplitIgnoreCase(this string input, string pattern)
Splits the input string at the position defined by pattern. The pattern is matched using an invariant-culture case-insensitive match.
string
RemoveCommonIndentation(this string str)
Removes spaces from the beginning of every line in such a way that the smallest indentation is reduced to zero. Lines which contain only whitespace are not considered in the calculation and may therefore become empty.
string
Repeat(this string input, int numTimes)
Concatenates the specified number of repetitions of the current string.
string
Replace(this string str, string oldValue, string newValue, StringComparison comparison)
Returns a new string in which all occurrences of oldValue in the current instance, identified using the specified string comparison, are replaced with newValue.
IEnumerable<string>
Split(this string str, int chunkSize)
Splits a string into chunks of equal size. The last chunk may be smaller than chunkSize, but all chunks, if any, will contain at least 1 character.
string[]
Split(this string str, params string[] separator)
Returns a string array that contains the substrings in this string that are delimited by elements of a specified string array.
string[]
SplitNoEmpty(this string str, params string[] separator)
Returns a string array that contains the substrings in this string that are delimited by elements of a specified string array. Empty items (zero-length strings) are filtered out.
string
SqlEscape(this string input)
Returns an SQL-compatible representation of the string in single-quotes with the appropriate characters escaped.
bool
StartsWith(this string str, char? ch)
Returns true if and only if this string starts with the specified character.
bool
StartsWithIgnoreCase(this string haystack, string value)
Determines whether the beginning of this string instance matches the specified string using the ordinal case-insensitive comparison (StringComparison.OrdinalIgnoreCase).
string
SubstringRight(this string source, int startIndex)
Same as string.Substring(int) but counting from the right instead of left. Equivalent to source.Reverse().Substring(startIndex).Reverse().
string
SubstringRight(this string source, int startIndex, int length)
Same as string.Substring(int, int) but counting from the right instead of left. Equivalent to source.Reverse().Substring(startIndex, length).Reverse().
string
SubstringRightSafe(this string source, int startIndex)
Same as StringExtensions.SubstringSafe(this string, int) but counting from the right instead of left. Equivalent to source.Reverse().SubstringSafe(startIndex).Reverse().
string
SubstringRightSafe(this string source, int startIndex, int length)
Same as StringExtensions.SubstringSafe(this string, int, int) but counting from the right instead of left. Equivalent to source.Reverse().SubstringSafe(startIndex, length).Reverse().
string
SubstringSafe(this string source, int startIndex)
Same as string.Substring(int) but does not throw exceptions when the start index falls outside the boundaries of the string. Instead the result is truncated as appropriate.
string
SubstringSafe(this string source, int startIndex, int length)
Same as string.Substring(int, int) but does not throw exceptions when the start index or length (or both) fall outside the boundaries of the string. Instead the result is truncated as appropriate.
byte[]
ToUtf16(this string input)
Converts the specified string to UTF-16.
byte[]
ToUtf16BE(this string input)
Converts the specified string to UTF-16 (Big Endian).
byte[]
ToUtf8(this string input)
Converts the specified string to UTF-8.
IEnumerable<string>
Trim(this IEnumerable<string> values)
Returns the specified collection, but with leading and trailing empty strings and nulls removed.
string
UnifyLineEndings(this string input)
Attempts to detect Unix-style and Mac-style line endings and converts them to Windows (\r\n).
string
Unindent(this string str)
Removes the overall indentation of the specified string while maintaining the relative indentation of each line.
string
UrlEscape(this string input)
Escapes all necessary characters in the specified string so as to make it usable safely in a URL.
bool
UrlStartsWith(this string url, string path)
Determines whether the specified URL starts with the specified URL path. For example, the URL "/directory/file" starts with "/directory" but not with "/dir".
string
UrlUnescape(this string input)
Reverses the escaping performed by StringExtensions.UrlEscape(this string) by decoding hexadecimal URL escape sequences into their original characters.
int
Utf16Length(this string input)
Determines the length of the UTF-16 encoding of the specified string.
int
Utf8Length(this string input)
Determines the length of the UTF-8 encoding of the specified string.
IEnumerable<string>
WordWrap(this string text, int maxWidth, int hangingIndent = 0)
Word-wraps the current string to a specified width. Supports UNIX-style newlines and indented paragraphs. (see also remarks)

Static fields

stringContains the set of characters that are used in base64-url encoding.