Table of Contents

Class StringExtensions

Namespace
AngleSharp.Text
Assembly
AngleSharp.dll

Useful methods for string objects.

public static class StringExtensions
Inheritance
StringExtensions
Inherited Members

Methods

Collapse(string)

Collapses all spaces in the given string.

public static string Collapse(this string str)

Parameters

str string

The string to collapse.

Returns

string

The modified string with collapsed spaces.

CollapseAndStrip(string)

Collapses and strips all spaces in the given string.

public static string CollapseAndStrip(this string str)

Parameters

str string

The string to collapse and strip.

Returns

string

The modified string with collapsed and stripped spaces.

Contains(string[], string, StringComparison)

Examines if a the given list of string contains a certain element.

public static bool Contains(this string[] list, string element, StringComparison comparison = StringComparison.Ordinal)

Parameters

list string[]

The list of strings.

element string

The element to search for.

comparison StringComparison

The default comparison to use.

Returns

bool

The status of the check.

CssFunction(string, string)

Creates a CSS function from the string with the given argument.

public static string CssFunction(this string value, string argument)

Parameters

value string

The CSS function name.

argument string

The CSS function argument.

Returns

string

The CSS function string.

CssString(string)

Serializes the string to a CSS string.

public static string CssString(this string value)

Parameters

value string

The value to serialize.

Returns

string

The CSS string representation.

FromDec(string)

Converts the given string to an integer.

public static int FromDec(this string s)

Parameters

s string

The decimal representation.

Returns

int

The integer number.

FromHex(string)

Converts the given string to an integer.

public static int FromHex(this string s)

Parameters

s string

The hexadecimal representation.

Returns

int

The integer number.

Has(string?, char, int)

Checks if the given string has a certain character at a specific index. The index is optional (default is 0).

public static bool Has(this string? value, char chr, int index = 0)

Parameters

value string

The value to examine.

chr char

The character to look for.

index int

The index of the character.

Returns

bool

True if the value has the char, otherwise false.

HasHyphen(string, string, StringComparison)

Checks if the provided string starts with the given value, either by exactly matching it, or by comparing against the start including an additional dash character.

public static bool HasHyphen(this string str, string value, StringComparison comparison = StringComparison.Ordinal)

Parameters

str string

The string to examine.

value string

The value to check against.

comparison StringComparison

The string comparison mode.

Returns

bool

True if the string is exactly equal to or starts with the given value.

HtmlEncode(string, Encoding)

Replaces characters in names and values that cannot be expressed by using the given encoding with &#...; base-10 unicode point.

public static string HtmlEncode(this string value, Encoding encoding)

Parameters

value string

The value to sanatize.

encoding Encoding

The encoding to consider.

Returns

string

The sanatized value.

HtmlLower(string)

Transforms the given string to lower case by the HTML specification.

public static string HtmlLower(this string value)

Parameters

value string

The string to be transformed.

Returns

string

The resulting string.

Is(string?, string?)

Checks if two strings are exactly equal.

public static bool Is(this string? current, string? other)

Parameters

current string

The current string.

other string

The other string.

Returns

bool

True if both are equal, false otherwise.

IsCustomElement(string)

Checks if the given string satisfies the rules for a custom element name.

public static bool IsCustomElement(this string tag)

Parameters

tag string

The current tag name.

Returns

bool

True if the string matches a custom element name.

IsOneOf(string?, string, string)

Examines if the given element is equal to one of the given elements.

public static bool IsOneOf(this string? element, string item1, string item2)

Parameters

element string

The element to check for equality.

item1 string

The first item to compare to.

item2 string

The second item to compare to.

Returns

bool

True if the element is equal to one of the elements, otherwise false.

IsOneOf(string, string, string, string)

Examines if the given element is equal to one of the given elements.

public static bool IsOneOf(this string element, string item1, string item2, string item3)

Parameters

element string

The element to check for equality.

item1 string

The first item to compare to.

item2 string

The second item to compare to.

item3 string

The third item to compare to.

Returns

bool

True if the element is equal to one of the elements, otherwise false.

IsOneOf(string, string, string, string, string)

Examines if the given element is equal to one of the given elements.

public static bool IsOneOf(this string element, string item1, string item2, string item3, string item4)

Parameters

element string

The element to check for equality.

item1 string

The first item to compare to.

item2 string

The second item to compare to.

item3 string

The third item to compare to.

item4 string

The fourth item to compare to.

Returns

bool

True if the element is equal to one of the elements, otherwise false.

IsOneOf(string, string, string, string, string, string)

Examines if the given element is equal to one of the given elements.

public static bool IsOneOf(this string element, string item1, string item2, string item3, string item4, string item5)

Parameters

element string

The element to check for equality.

item1 string

The first item to compare to.

item2 string

The second item to compare to.

item3 string

The third item to compare to.

item4 string

The fourth item to compare to.

item5 string

The fifth item to compare to.

Returns

bool

True if the element is equal to one of the elements, otherwise false.

Isi(string?, string?)

Checks if two strings are equal when viewed case-insensitive.

public static bool Isi(this string? current, string? other)

Parameters

current string

The current string.

other string

The other string.

Returns

bool

True if both are equal, false otherwise.

NormalizeLineEndings(string)

Replaces every occurrence of a "CR" (U+000D) character not followed by a "LF" (U+000A) character, and every occurrence of a "LF" (U+000A) character not preceded by a "CR" (U+000D) character, by a two-character string consisting of a U+000D CARRIAGE RETURN "CRLF" (U+000A) character pair.

public static string NormalizeLineEndings(this string value)

Parameters

value string

The value to normalize.

Returns

string

The normalized string.

ParseSecuritySettings(string, bool)

Converts the given value to a sandbox flag.

public static Sandboxes ParseSecuritySettings(this string value, bool allowFullscreen = false)

Parameters

value string

The value to convert.

allowFullscreen bool

Should full screen be allowed?

Returns

Sandboxes

The sandbox flag.

ReplaceFirst(string, string, string)

Replaces the first occurance of the string search with replace.

public static string ReplaceFirst(this string text, string search, string replace)

Parameters

text string

The text to use.

search string

The string to search for.

replace string

The one-time replacement string.

Returns

string

The result of the search-and-replace.

SplitCommas(string)

Splits the string on commas.

public static string[] SplitCommas(this string str)

Parameters

str string

The string to examine.

Returns

string[]

The list of tokens.

SplitSpaces(string)

Splits the string on space characters.

public static string[] SplitSpaces(this string str)

Parameters

str string

The string to examine.

Returns

string[]

The list of tokens.

SplitWithTrimming(string, char)

Splits the string with the given char delimiter and trims the leading and tailing spaces.

public static string[] SplitWithTrimming(this string str, char ch)

Parameters

str string

The string to examine.

ch char

The delimiter character.

Returns

string[]

The list of tokens.

SplitWithoutTrimming(string, char)

Splits the char array with the given char delimiter.

public static string[] SplitWithoutTrimming(this string str, char c)

Parameters

str string

The string to examine.

c char

The delimiter character.

Returns

string[]

The list of tokens.

StripLeadingTrailingSpaces(string)

Strips all leading and trailing space characters from the given char array.

public static string StripLeadingTrailingSpaces(this string str)

Parameters

str string

The string to examine.

Returns

string

A new string, which excludes the leading and tailing spaces.

StripLineBreaks(string)

Strips all line breaks from the given string.

public static string StripLineBreaks(this string str)

Parameters

str string

The string to examine.

Returns

string

A new string, which excludes the line breaks.

ToBoolean(string?, bool)

Converts the given value to a boolean (or not).

public static bool ToBoolean(this string? value, bool defaultValue = false)

Parameters

value string

The value to convert.

defaultValue bool

The default value to consider (optional).

Returns

bool

The converted boolean.

ToDouble(string?, double)

Converts the given value to a double (or not).

public static double ToDouble(this string? value, double defaultValue = 0)

Parameters

value string

The value to convert.

defaultValue double

The default value to consider (optional).

Returns

double

The converted double.

ToEncodingType(string?)

Converts the current string to one of the encoding types.

public static string? ToEncodingType(this string? encType)

Parameters

encType string

The string to convert.

Returns

string

The valid encoding type string or null.

ToEnum<T>(string?, T)

Converts the given value to an enumeration value (or not).

public static T ToEnum<T>(this string? value, T defaultValue) where T : struct, Enum

Parameters

value string

The value to convert.

defaultValue T

The default value to consider (optional).

Returns

T

The converted enum value.

Type Parameters

T

ToFormMethod(string?)

Converts the current string to one of the form methods.

public static string? ToFormMethod(this string? method)

Parameters

method string

The string to convert.

Returns

string

The valid form method string or null.

ToInteger(string?, int)

Converts the given value to an integer (or not).

public static int ToInteger(this string? value, int defaultValue = 0)

Parameters

value string

The value to convert.

defaultValue int

The default value to consider (optional).

Returns

int

The converted integer.

ToInteger(string?, uint)

Converts the given value to an unsigned integer (or not).

public static uint ToInteger(this string? value, uint defaultValue = 0)

Parameters

value string

The value to convert.

defaultValue uint

The default value to consider (optional).

Returns

uint

The converted unsigned integer.

UrlDecode(string)

Decodes the provided percent encoded string. An exception is thrown in case of an invalid input value.

public static byte[] UrlDecode(this string value)

Parameters

value string

The value to decode.

Returns

byte[]

The decoded content.

UrlEncode(byte[])

Replaces characters in names and values that should not be in URL values. Replaces the bytes 0x20 (U+0020 SPACE if interpreted as ASCII) with a single 0x2B byte ("+" (U+002B) character if interpreted as ASCII). If a byte is not in the range 0x2A, 0x2D, 0x2E, 0x30 to 0x39, 0x41 to 0x5A, 0x5F, 0x61 to 0x7A, it is replaced with its hexadecimal value (zero-padded if necessary), starting with the percent sign.

public static string UrlEncode(this byte[] content)

Parameters

content byte[]

The content to encode.

Returns

string

The encoded value.