开放的编程资料库

当前位置:我爱分享网 > C#教程 > 正文

C# 字素

C#字素教程展示了如何在C#中使用字素。

字素

字素是任何给定语言书写系统的最小单位。一个单独的字素本身可能具有也可能不具有意义,并且可能对应于也可能不对应于口语中的单个音素。

术语字符已用于表示原始ASCII表中的单个字符。但是,该表只能表示一组有限的字符。已创建AUnicode标准来处理其他文本值。

统一码

Unicode是一种计算行业标准,用于对世界上大多数书写系统中表达的文本进行一致的编码、表示和处理。

在C#中,字符串是Unicode字符的序列。它是一种存储数据值序列的数据类型,通常是字节,其中元素通常根据字符编码代表字符。C#内部使用UTF-16编码。

在ASCII表之外,最好使用术语字素而不是术语字符。.NET平台将文本元素定义为显示为字素的文本单元。TextElementEnumerator枚举字符串的文本元素。文本元素可以是基本字符、代理对或组合字符序列。

代码点是字符集中的数字偏移量。每个代码点都是一个数字,其含义由Unicode标准给出。例如,拉丁字母Q是U+0051码位,西里尔小写字母zheж是U+0436码位。

字素簇是一个或多个代码点的序列,这些代码点显示为单个图形单元。例如,印地语字母त०包含两个码位:U+0924代表त和U+0947代表à¥

bytes是为字符串内容存储的实际信息。根据所使用的Unicode标准(UTF-8、UTF-16等),每个代码点可能需要一个或多个字节的存储空间。).

代理对

C#使用UTF-16编码方案来存储Unicode文本字符串。在UTF-16中,使用16位(两字节)代码单元。由于16位只能包含从0x0到0xFFFF的字符范围,因此额外的字节用于存储超出此范围(0x10000到0x10FFFF)的值。这是使用代理项对完成的。

代理对是单个抽象字符的编码字符表示,由两个代码单元的序列组成。该对的第一个单元是高代理,第二个是低代理。组合字符序列作为基本字符和一个或多个组合字符的组合。代理对可以代表一个基本字符或一个组合字符。

C#字素示例

在下面的示例中,我们使用字素。

using System.Text;
using System.Globalization;

Console.OutputEncoding = System.Text.Encoding.UTF8;

Console.WriteLine("The Hindi word Namaste");

string word = "नमस्ते";
Console.WriteLine(word);
Console.WriteLine();

// code points

Console.WriteLine("Code points:");

for (int i = 0; i < word.Length; i += Char.IsSurrogatePair(word, i) ? 2 : 1)
{
    int x = Char.ConvertToUtf32(word, i);

    Console.WriteLine("U+{0:X4} {1}", x, Char.ConvertFromUtf32(x));
}

Console.WriteLine();

// bytes

Console.WriteLine("Bytes: ");
byte[] bytes = Encoding.UTF8.GetBytes(word);

foreach (byte c in bytes)
{
    Console.Write($"{c} ");
}

Console.WriteLine("\n");

// graphemes

Console.WriteLine("Graphemes: ");

int count = 0;

TextElementEnumerator graphemeEnum = StringInfo.GetTextElementEnumerator(word);
while (graphemeEnum.MoveNext())
{
    string grapheme = graphemeEnum.GetTextElement();

    Console.WriteLine(grapheme);

    count++;
}

Console.WriteLine($"the word has {count} graphemes");

该示例定义了一个包含印地语单词namaste的变量。我们打印单词,打印它的代码点、字节,并打印和计算字素的数量。

Console.OutputEncoding = System.Text.Encoding.UTF8;

为了将Unicode字符输出到终端,我们将控制台输出编码设置为UTF8。

string word = "नमस्ते";
Console.WriteLine(word);

我们有印地语namaste词;我们将它打印到控制台。

// code points

Console.WriteLine("Code points:");

for (int i = 0; i < word.Length; i += Char.IsSurrogatePair(word, i) ? 2 : 1)
{
    int x = Char.ConvertToUtf32(word, i);

    Console.WriteLine("U+{0:X4} {1}", x, Char.ConvertFromUtf32(x));
}

我们打印单词的代码点。Lenth属性决定字符串中UTF-16字符的数量。Char.IsSurrogatePair方法用于判断字符串中指定位置的两个相邻Char对象是否构成代理对。如果是这样,我们需要更多的字节来表示一个字素。

使用Char.ConvertToUft32方法,我们打印Unicode代码点;该方法将字符串中指定位置的UTF-16编码字符或代理项对的值转换为Unicode代码点。最后,Char.ConvertFromUtf32方法将给定的Unicode编码点转换为UTF-16编码的字符串;我们得到了字素。

Console.WriteLine("Bytes: ");
byte[] bytes = Encoding.UTF8.GetBytes(word);

foreach (byte c in bytes)
{
    Console.Write($"{c} ");
}

Console.WriteLine("\n");

我们打印存储在磁盘上的单词的实际字节数。我们使用Encoding.UTF8.GetBytes方法获取底层字节数组。

Console.WriteLine("Graphemes: ");

int count = 0;

TextElementEnumerator graphemeEnum = StringInfo.GetTextElementEnumerator(word);
while (graphemeEnum.MoveNext())
{
    string grapheme = graphemeEnum.GetTextElement();

    Console.WriteLine(grapheme);

    count++;
}

Console.WriteLine($"the word has {count} graphemes");

我们打印作品的字素并计算它们。TextElementEnumerator用于枚举单词的字素。GetTextElement用于获取当前文本元素(字素)。

$ dotnet run
The Hindi word Namaste
नमस्ते

Code points:
U+0928 न
U+092E म
U+0938 स
U+094D  ्
U+0924 त
U+0947  े

Bytes:
224 164 168 224 164 174 224 164 184 224 165 141 224 164 164 224 165 135

Graphemes:
न
म
स्
ते
the word has 4 graphemes

在本文中,我们使用了字素、代码点、字节和Unicode字符串的代理对。

使用Unicode的有用站点:www.fileformat.info和www.utf8-chartable.de。

列出所有C#教程。

未经允许不得转载:我爱分享网 » C# 字素

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏