Rune Struct
Namespace: SystemSummary
Represents a Unicode scalar value.
Description
A Rune
is a Unicode scalar value, which is a numerical value assigned to every character in the Unicode standard. This struct provides a way to work with individual Unicode code points in a safe and efficient manner. It is particularly useful when dealing with strings that may contain multi-byte characters or surrogate pairs.
The Rune
struct implements the IComparable, IComparable<T>, IEquatable<T>, and ISpanFormattable interfaces, allowing for comparisons, equality checks, and formatting within spans.
Fields
-
readonly System.Text.Rune Null
Represents the null rune, which is equivalent to the Unicode replacement character (U+FFFD).
Constructors
-
Rune(char c)
Initializes a new instance of the Rune struct by using the specified character.Parameters
c
: The character to represent as a rune.
- ArgumentException: If the specified character is not a valid Unicode scalar value (i.e., it's a surrogate code unit).
-
Rune(uint codePoint)
Initializes a new instance of the Rune struct by using the specified Unicode code point.Parameters
codePoint
: The Unicode code point to represent as a rune.
- ArgumentException: If the specified code point is not a valid Unicode scalar value.
Methods
-
int CompareTo(System.Text.Rune other)
Compares the current rune with another rune.
-
int CompareTo(object? obj)
Compares the current rune with another object.
-
bool Equals(System.Text.Rune other)
Indicates whether the current rune is equal to another rune.
-
bool Equals(object? obj)
Indicates whether the current rune is equal to another object.
-
int GetHashCode()
Returns the hash code for the current rune.
-
string ToString()
Converts the current rune to its string representation.
-
bool TryGetCodePoint(out uint codePoint)
Attempts to get the Unicode code point of the current rune.
-
bool IsControl()
Indicates whether the current rune is a control character.
-
bool IsDigit()
Indicates whether the current rune is a digit character.
-
bool IsLetter()
Indicates whether the current rune is a letter character.
-
bool IsLowSurrogate()
Indicates whether the current rune is a low surrogate code unit.
-
bool IsHighSurrogate()
Indicates whether the current rune is a high surrogate code unit.
-
bool IsSurrogate()
Indicates whether the current rune is a surrogate code unit.
-
bool IsSymbol()
Indicates whether the current rune is a symbol character.
-
bool IsPunctuation()
Indicates whether the current rune is a punctuation character.
-
bool IsWhiteSpace()
Indicates whether the current rune is a whitespace character.
Operators
-
bool operator ==(System.Text.Rune left, System.Text.Rune right)
Compares two runes for equality.
-
bool operator !=(System.Text.Rune left, System.Text.Rune right)
Compares two runes for inequality.
-
int operator <(System.Text.Rune left, System.Text.Rune right)
Compares two runes for less than.
-
int operator >(System.Text.Rune left, System.Text.Rune right)
Compares two runes for greater than.
-
int operator <=(System.Text.Rune left, System.Text.Rune right)
Compares two runes for less than or equal to.
-
int operator >=(System.Text.Rune left, System.Text.Rune right)
Compares two runes for greater than or equal to.
Example
using System;
public class RuneExample
{
public static void Main(string[] args)
{
// Create a Rune from a character
Rune apostrophe = new Rune('\'');
Console.WriteLine($"Rune for apostrophe: {apostrophe}"); // Output: Rune for apostrophe: '
// Create a Rune from a code point
// Unicode code point for 'A' is U+0041
Rune capitalA = new Rune(0x0041);
Console.WriteLine($"Rune for U+0041: {capitalA}"); // Output: Rune for U+0041: A
// Check if a rune is a digit
Rune digitFive = new Rune('5');
if (digitFive.IsDigit())
{
Console.WriteLine("'5' is a digit."); // Output: '5' is a digit.
}
// Get the code point of a rune
if (apostrophe.TryGetCodePoint(out uint codePoint))
{
Console.WriteLine($"The code point for '{apostrophe}' is U+{codePoint:X4}"); // Output: The code point for ''' is U+0027
}
// Compare runes
Rune b = new Rune('b');
if (b > apostrophe)
{
Console.WriteLine("'b' comes after '."); // Output: 'b' comes after '.
}
}
}