In This Article

CharClass Class

Stores information about a character class.

public class CharClass
Inheritance:
object object

Constructors

CharClass()

Initializes an instance of the class.

public CharClass()

CharClass(CharClass)

Initializes an instance of the class with another CharClass.

public CharClass(CharClass charClass)
Parameter Type Description
charClass CharClass

The CharClass whose items should be added.

CharClass(char)

Initializes an instance of the class with a single character.

public CharClass(char ch)
Parameter Type Description
ch char

A character to add to the character class.

CharClass(char, bool)

Initializes an instance of the class with a single character and an optional complement.

public CharClass(char ch, bool isNegated)
Parameter Type Description
ch char

A character to add to the character class.

isNegated bool

Whether to negate the character class after adding the initial character.

CharClass(char, char)

Initializes an instance of the class with an interval of characters.

public CharClass(char start, char end)
Parameter Type Description
start char

The start character in the interval.

end char

The end character in the interval.

CharClass(char, char, bool)

Initializes an instance of the class with an interval of characters and an optional complement.

public CharClass(char start, char end, bool isNegated)
Parameter Type Description
start char

The start character in the interval.

end char

The end character in the interval.

isNegated bool

Whether to negate the character class after adding the initial character range.

CharClass(IEnumerable<CharInterval>)

Initializes an instance of the class with an enumerable of CharInterval objects.

public CharClass(IEnumerable<CharInterval> intervals)
Parameter Type Description
intervals IEnumerable<CharInterval>

The enumerable of CharInterval objects that should be added.

CharClass(string)

Initializes an instance of the class with a string of characters.

public CharClass(string characters)
Parameter Type Description
characters string

The string of characters to add to the character class.

Properties

All

A CharClass that contains all Unicode characters.

public static CharClass All { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of [\u0000-\uFFFF].

Alpha

A CharClass that contains all alphabetic characters.

public static CharClass Alpha { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of \p{L} (all letters).

CharacterCount

The number of characters contained in the CharClass intervals, excluding any Unicode general categories specified.

public int CharacterCount { get; }

Property Value

int

Count

The number of character intervals contained in the CharClass instance.

public int Count { get; }

Property Value

int

Digit

A CharClass that contains all Unicode decimal digits.

public static CharClass Digit { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of \d and \p{Nd}.

HasCategorySpecification

Whether there is at least one Unicode generate category specification in the character class.

public bool HasCategorySpecification { get; }

Property Value

bool:

true if there is at least one Unicode generate category specification in the character class; otherwise, false.

Remarks

Use the GetCategorySpecification(UnicodeCategory) method to test whether a specific UnicodeCategory is specified, and whether it is positive or negative.

HexDigit

A CharClass that contains all Unicode hexadecimal digits.

public static CharClass HexDigit { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of [0-9a-fA-F].

IsEmpty

Whether there are no Unicode categories or intervals specified in the character class.

public bool IsEmpty { get; }

Property Value

bool:

true if there are no Unicode categories or intervals specified in the character class; otherwise, false.

IsNegated

Whether the character class is negated.

public bool IsNegated { get; set; }

Property Value

bool:

true if the character class is negated; otherwise, false.

Remarks

The negation is the opposite of the character class, specified by a ^ character after the open square brace. When negated, the return value of Contains(char) is toggled.

IsReadOnly

Gets a value indicating whether the ICollection<T> is read-only.

public bool IsReadOnly { get; }

Property Value

bool:

true if the ICollection<T> is read-only; otherwise, false.

IsSingleCharacter

Whether the character class only contains an interval for a single character.

public bool IsSingleCharacter { get; }

Property Value

bool:

true if the character class only contains an interval for a single character; otherwise, false.

this[int]

The CharInterval at the specified index.

public CharInterval this[int index] { get; }
Parameter Type Description
index int

The index of the interval to return.

Property Value

CharInterval

LineTerminator

A CharClass that contains all Unicode line terminators.

public static CharClass LineTerminator { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of [\n\r\p{Zl}\p{Zp}].

LineTerminatorWhitespace

A CharClass that contains all Unicode line terminators and whitespace characters.

public static CharClass LineTerminatorWhitespace { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of \s or [\f\n\r\t\v\x85\p{Z}].

NonAlpha

A CharClass that contains no alphabetic digits.

public static CharClass NonAlpha { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of \P{L} (no letters).

NonDigit

A CharClass that contains no Unicode decimal digits.

public static CharClass NonDigit { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of \D.

NonHexDigit

A CharClass that contains no Unicode hexadecimal digits.

public static CharClass NonHexDigit { get; }

Property Value

CharClass

NonLineTerminator

A CharClass that contains no Unicode line terminators.

public static CharClass NonLineTerminator { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of [^\n\r\p{Zl}\p{Zp}].

NonLineTerminatorWhitespace

A CharClass that contains no Unicode line terminators or whitespace characters.

public static CharClass NonLineTerminatorWhitespace { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of \S or [^\f\n\r\t\v\x85\p{Z}].

NonWhitespace

A CharClass that contains no Unicode whitespace characters.

public static CharClass NonWhitespace { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of [^\f\t\v\x85\p{Zs}].

NonWord

A CharClass that contains no word characters.

public static CharClass NonWord { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of \W and [^\p{L}\p{Nd}\p{Pc}] (no letters, decimal digits, and connectors like underscore).

None

A CharClass that contains no characters.

public static CharClass None { get; }

Property Value

CharClass

Whitespace

A CharClass that contains all Unicode whitespace characters.

public static CharClass Whitespace { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of [\f\t\v\x85\p{Zs}].

Word

A CharClass that contains all word characters.

public static CharClass Word { get; }

Property Value

CharClass

Remarks

This character class is the equivalent of \w and [\p{L}\p{Nd}\p{Pc}] (all letters, decimal digits, and connectors like underscore).

Methods

Add(CharInterval)

Adds a character interval to the character class.

public void Add(CharInterval interval)
Parameter Type Description
interval CharInterval

The CharInterval to add.

Add(char)

Adds a single character to the character class.

public void Add(char ch)
Parameter Type Description
ch char

The character to add.

Add(char, char)

Adds an interval of characters to the character class.

public void Add(char start, char end)
Parameter Type Description
start char

The start character in the interval.

end char

The end character in the interval.

AddCategory(UnicodeCategory, bool)

Adds the specified Unicode general category to the character class.

public void AddCategory(UnicodeCategory category, bool negate = false)
Parameter Type Description
category UnicodeCategory

A UnicodeCategory indicating the category.

negate bool

Whether to negate the category. The default value is false.

AddCategory(string, bool)

Adds the specified Unicode general category to the character class.

public void AddCategory(string categoryName, bool negate = false)
Parameter Type Description
categoryName string

The name of the category. For example, Lu is used for uppercase letters.

negate bool

Whether to negate the category. The default value is false.

AddRange(CharClass)

Adds the contents of a CharClass to the collection.

public void AddRange(CharClass charClass)
Parameter Type Description
charClass CharClass

The CharClass whose values are to be added.

AddRange(IEnumerable<CharInterval>)

Adds the contents of a CharInterval enumerable to the collection.

public void AddRange(IEnumerable<CharInterval> intervals)
Parameter Type Description
intervals IEnumerable<CharInterval>

The CharInterval enumerable whose values are to be added.

CanMergeWith(CharClass)

Returns whether the CharClass can successfully merge with another specified CharClass without any possible issues.

public bool CanMergeWith(CharClass other)
Parameter Type Description
other CharClass

The CharClass to examine.

Returns

bool:

true if the CharClass instances can merge via AddRange(CharClass) without any possible issues; otherwise, false.

Clear()

Clears the characters in this CharClass instance, but does not reset the IsNegated flag.

public void Clear()

Contains(CharClass)

Returns whether the specified character class is in the character class.

public bool Contains(CharClass charClass)
Parameter Type Description
charClass CharClass

The CharClass to look for in the collection.

Returns

bool:

true if the character class is in the collection; otherwise, false.

Contains(CharInterval)

Returns whether all of the characters in the specified interval are in the character class.

public bool Contains(CharInterval interval)
Parameter Type Description
interval CharInterval

The CharInterval to examine.

Returns

bool:

true if all of the characters in the specified interval are in the character class; otherwise, false.

Contains(char)

Returns whether the specified character is in the character class.

public bool Contains(char ch)
Parameter Type Description
ch char

The character to look for in the character class.

Returns

bool:

true if the character is in the character class; otherwise, false.

Contains(char, char)

Returns whether the specified interval of characters is in the character class.

public bool Contains(char start, char end)
Parameter Type Description
start char

The start character in the interval.

end char

The end character in the interval.

Returns

bool:

true if the interval of characters is in the collection; otherwise, false.

CopyTo(CharInterval[], int)

Copies the elements of the ICollection<T> to an Array, starting at a particular Array index.

public void CopyTo(CharInterval[] array, int arrayIndex)
Parameter Type Description
array CharInterval[]

The one-dimensional Array that is the destination of the elements copied from ICollection<T>. The Array must have zero-based indexing.

arrayIndex int

The zero-based index in array at which copying begins.

Exceptions

Type Condition
ArgumentNullException

array is null.

ArgumentOutOfRangeException

arrayIndex is less than 0.

ArgumentException

The number of elements in the source ICollection<T> is greater than the available space from arrayIndex to the end of the destination array.

Equals(object?)

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)
Parameter Type Description
obj object

The object to compare with the current object.

Returns

bool:

true if the specified object is equal to the current object; otherwise, false.

GetCategorySpecification(UnicodeCategory)

Returns whether a certain Unicode general category is specified in this character class, and if so, whether it is positive or negative matching.

public bool? GetCategorySpecification(UnicodeCategory category)
Parameter Type Description
category UnicodeCategory

A UnicodeCategory indicating the category.

Returns

bool?:

true if the category is specified as a positive match. false if the category is specified as a negative match. null if the category is not specified.

GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<CharInterval> GetEnumerator()

Returns

IEnumerator<CharInterval>:

An enumerator that can be used to iterate through the collection.

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int:

A hash code for the current object.

Remove(CharInterval)

Removes the specified character interval from the character class.

public bool Remove(CharInterval interval)
Parameter Type Description
interval CharInterval

The CharInterval to remove.

Returns

bool:

true if the character interval was removed; otherwise, false.

Remove(char)

Removes the specified character from the character class.

public bool Remove(char ch)
Parameter Type Description
ch char

The character to remove.

Returns

bool:

true if the character was removed; otherwise, false.

RemoveCategory(UnicodeCategory)

Removes the specified Unicode general category from the character class.

public void RemoveCategory(UnicodeCategory category)
Parameter Type Description
category UnicodeCategory

A UnicodeCategory indicating the category.

RemoveCategory(string)

Removes the specified Unicode general category from the character class.

public void RemoveCategory(string categoryName)
Parameter Type Description
categoryName string

The name of the category. For example, "Lu" is used for uppercase letters.

ToString()

Returns the string representation of this object.

public override string ToString()

Returns

string:

The string representation of this object.

Inherited Members

Extension Methods