System.Text Namespace

System.Text 命名空间包含表示 ASCII 和 Unicode 字符编码的类;用于让字符块和字节块相互转换的抽象基类;以及无需创建 String 的中间实例即可操作并格式化 String 对象的帮助器类。 The System.Text namespace contains classes that represent ASCII and Unicode character encodings; abstract base classes for converting blocks of characters to and from blocks of bytes; and a helper class that manipulates and formats String objects without creating intermediate instances of String.

Classes

ASCIIEncoding

表示 Unicode 字符的 ASCII 字符编码。Represents an ASCII character encoding of Unicode characters.

CodePagesEncodingProvider

为只在桌面 .NET Framework 中可用的代码页提供对编码提供程序的访问权限。Provides access to an encoding provider for code pages that otherwise are available only in the desktop .NET Framework.

Decoder

将已编码字节的序列转换为一组字符。Converts a sequence of encoded bytes into a set of characters.

DecoderExceptionFallback

为不能转换为输入字符的已编码输入字节序列提供失败处理机制(称为“回退”)。Provides a failure-handling mechanism, called a fallback, for an encoded input byte sequence that cannot be converted to an input character. 回退引发异常,而不是解码输入字节序列。The fallback throws an exception instead of decoding the input byte sequence. 无法继承此类。This class cannot be inherited.

DecoderExceptionFallbackBuffer

当编码的输入字节序列无法转换为解码的输出字符时引发 DecoderFallbackExceptionThrows DecoderFallbackException when an encoded input byte sequence cannot be converted to a decoded output character. 无法继承此类。This class cannot be inherited.

DecoderFallback

为不能转换为输出字符的已编码输入字节序列提供称为“回退”的失败处理机制。Provides a failure-handling mechanism, called a fallback, for an encoded input byte sequence that cannot be converted to an output character.

DecoderFallbackBuffer

提供一个缓冲区,该缓冲区允许回退处理程序在解码器无法对输入字节序列进行解码时将替换字符串返回到该解码器。Provides a buffer that allows a fallback handler to return an alternate string to a decoder when it cannot decode an input byte sequence.

DecoderFallbackException

解码器回退操作失败时引发的异常。The exception that is thrown when a decoder fallback operation fails. 无法继承此类。This class cannot be inherited.

DecoderReplacementFallback

为不能转换为输出字符的已编码输入字节序列提供称为“回退”的失败处理机制。Provides a failure-handling mechanism, called a fallback, for an encoded input byte sequence that cannot be converted to an output character. 回退发出用户指定的替换字符串,而不是已解码的输入字节序列。The fallback emits a user-specified replacement string instead of a decoded input byte sequence. 无法继承此类。This class cannot be inherited.

DecoderReplacementFallbackBuffer

表示无法对原始输入字节序列解码时发出的替代输出字符串。Represents a substitute output string that is emitted when the original input byte sequence cannot be decoded. 无法继承此类。This class cannot be inherited.

Encoder

将一组字符转换为一个字节序列。Converts a set of characters into a sequence of bytes.

EncoderExceptionFallback

为不能转换为输出字节序列的输入字符提供一个称为“回退”的失败处理机制。Provides a failure-handling mechanism, called a fallback, for an input character that cannot be converted to an output byte sequence. 如果输入字符无法转换为输出字节序列,则回退引发异常。The fallback throws an exception if an input character cannot be converted to an output byte sequence. 无法继承此类。This class cannot be inherited.

EncoderExceptionFallbackBuffer

当输入字符无法转换为编码的输出字节序列时引发 EncoderFallbackExceptionThrows EncoderFallbackException when an input character cannot be converted to an encoded output byte sequence. 无法继承此类。This class cannot be inherited.

EncoderFallback

为不能转换为已编码输出字节序列的输入字符提供称为“回退”的失败处理机制。Provides a failure-handling mechanism, called a fallback, for an input character that cannot be converted to an encoded output byte sequence.

EncoderFallbackBuffer

提供一个缓冲区,该缓冲区允许回退处理程序在编码器无法对输入字符进行编码时将替换字符串返回到该编码器。Provides a buffer that allows a fallback handler to return an alternate string to an encoder when it cannot encode an input character.

EncoderFallbackException

编码器回退操作失败时引发的异常。The exception that is thrown when an encoder fallback operation fails. 无法继承此类。This class cannot be inherited.

EncoderReplacementFallback

为不能转换为输出字节序列的输入字符提供一个称为“回退”的失败处理机制。Provides a failure handling mechanism, called a fallback, for an input character that cannot be converted to an output byte sequence. 此回退使用由用户指定的替换字符串来代替原始的输入字符。The fallback uses a user-specified replacement string instead of the original input character. 无法继承此类。This class cannot be inherited.

EncoderReplacementFallbackBuffer

表示无法对原始输入字符进行编码时使用的替代输入字符串。Represents a substitute input string that is used when the original input character cannot be encoded. 无法继承此类。This class cannot be inherited.

Encoding

表示字符编码。Represents a character encoding.

EncodingExtensions
EncodingInfo

提供有关编码的基本信息。Provides basic information about an encoding.

EncodingProvider

提供编码提供程序的基类,后者提供在特定平台上不可用的编码。Provides the base class for an encoding provider, which supplies encodings that are unavailable on a particular platform.

StringBuilder

表示可变字符字符串。Represents a mutable string of characters. 无法继承此类。This class cannot be inherited.

UnicodeEncoding

表示 Unicode 字符的 UTF-16 编码。Represents a UTF-16 encoding of Unicode characters.

UTF32Encoding

表示 Unicode 字符的 UTF-32 编码。Represents a UTF-32 encoding of Unicode characters.

UTF7Encoding

表示 Unicode 字符的 UTF-7 编码。Represents a UTF-7 encoding of Unicode characters.

UTF8Encoding

表示 Unicode 字符的 UTF-8 编码。Represents a UTF-8 encoding of Unicode characters.

Structs

Rune

表示 Unicode 标量值([ U+0000..U+D7FF ],含首尾值;或 [ U+E000..U+10FFFF ],含首尾值)。Represents a Unicode scalar value ([ U+0000..U+D7FF ], inclusive; or [ U+E000..U+10FFFF ], inclusive).

SpanRuneEnumerator

为由包含 UTF-16 文本的范围表示的 Rune 值提供枚举器。Provides an enumerator for the Rune values represented by a span containing UTF-16 text.

StringBuilder.ChunkEnumerator

支持对 StringBuilder 实例的区块进行简单迭代。Supports simple iteration over the chunks of a StringBuilder instance.

StringRuneEnumerator

为由字符串表示的 Rune 值提供枚举器。Provides an enumerator for the Rune values represented by a string.

Enums

NormalizationForm

定义要执行的规范化的类型。Defines the type of normalization to perform.

Remarks

编码类主要用于在不同的编码或代码页和 Unicode 编码之间转换。The encoding classes are primarily intended to convert between different encodings or code pages and a Unicode encoding. Encoding.Unicode (UTF-16)编码由 .NET Framework 在内部使用,并且 Encoding.UTF8 编码通常用于存储字符数据,以确保跨计算机和区域性实现可移植性。Encoding.Unicode (UTF-16) encoding is used internally by the .NET Framework, and Encoding.UTF8 encoding is often used for storing character data to ensure portability across machines and cultures.

派生自 Encoding 的类使你能够选择回退策略,该策略决定了如何处理无法编码为字节序列的字符或不能解码为字符的字节数。The classes derived from Encoding enable you to choose a fallback strategy, which determines how characters that cannot be encoded into a sequence of bytes, or bytes that cannot be decoded into characters, are handled. 您可以选择下列项之一:You can choose one of the following:

  • 异常回退。Exception fallback. 可以通过使用某些类构造函数中提供的 throwonerror 标志或使用 EncoderExceptionFallbackDecoderExceptionFallback 类,选择引发数据错误的异常。You can choose to throw exceptions on data errors either by using a throwonerror flag that is available in some class constructors or by using the EncoderExceptionFallback and DecoderExceptionFallback classes. 如果你担心数据流的完整性,则建议在出现异常时引发。If you are concerned about the integrity of the data stream, throwing on an exception is recommended.

  • 替换回退。Replacement fallback. 您可以使用 EncoderFallbackDecoderFallback 类以无提示方式将字符更改为 "?" 或 Unicode 替换字符(U + FFFD)。You can use the EncoderFallback and DecoderFallback classes to silently change a character to "?" or to the Unicode replacement character (U+FFFD).

  • 最佳回退。Best-fit fallback. 此选项将一种编码中的字符映射到另一编码的字符。This option maps a character in one encoding to a character in another encoding. 通常不建议使用最佳回退,因为这可能会导致数据丢失和混淆,并且比简单的 "?" 字符替换慢。Best fit fallback is often not recommended because it can cause data loss and confusion, and is slower than simple "?" character replacements. 但对于 ANSI 代码页,最佳行为是默认值。However, for ANSI code pages the best-fit behavior is the default.

StringBuilder 类用于对单个字符串执行大量操作的操作。The StringBuilder class is designed for operations that perform extensive manipulations on a single string. String 类不同,StringBuilder 类是可变的,在串联或删除字符串时提供更好的性能。Unlike the String class, the StringBuilder class is mutable and provides better performance when concatenating or deleting strings.

有关 System.Text的详细信息,请参阅.NET Framework 中的字符编码For more information about System.Text, see Character Encoding in the .NET Framework .