Namespace: System.Text

Provides fundamental types for manipulating strings and byte arrays, including encoding and decoding text.

StringBuilder Class

Class

Represents a mutable sequence of characters. This class is designed for performance when you need to perform a large number of appends or modifications to a string.

public sealed class StringBuilder : IEnumerable

Members:

  • Append(string value): Appends the string representation of a value to the end of this instance.
  • AppendLine(): Appends the default line terminator to the end of this instance.
  • Clear(): Removes all characters from the current StringBuilder instance.
  • ToString(): Converts this instance to a string.
// Example usage of StringBuilder StringBuilder sb = new StringBuilder(); sb.Append("Hello"); sb.Append(" "); sb.Append("World!"); Console.WriteLine(sb.ToString()); // Output: Hello World!

Encoding Class

Abstract Class

Represents a text encoding. Encoding and decoding can be performed by calling the appropriate methods of subclasses of the Encoding class.

public abstract class Encoding

Methods:

  • GetString(byte[] bytes): Converts an array of bytes to an equivalent string.
  • GetBytes(string s): Converts a string to an array of bytes.
  • GetChars(byte[] bytes): Converts an array of bytes to an array of characters.

Common Encodings:

  • Encoding.UTF8: Gets an instance of the UTF-8 encoding.
  • Encoding.Unicode: Gets an instance of the UTF-16 little-endian encoding.
  • Encoding.ASCII: Gets an instance of the ASCII encoding.
// Example usage of Encoding string originalString = "Hello, Encode!"; byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes(originalString); string decodedString = System.Text.Encoding.UTF8.GetString(utf8Bytes); Console.WriteLine(decodedString); // Output: Hello, Encode!

Decoder Class

Abstract Class

Decodes a sequence of byte arrays into a sequence of characters.

public abstract class Decoder

Methods:

  • GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex): Converts a range of bytes, starting at byteIndex and containing byteCount bytes, into a sequence of characters.

Encoder Class

Abstract Class

Encodes a sequence of characters into a sequence of byte arrays.

public abstract class Encoder

Methods:

  • GetByteCount(char[] chars, int index, int count): Returns the number of bytes produced by the Encoder.
  • GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush): Converts a range of characters into a sequence of bytes.

ASCIIEncoding Class

Class

Represents the ASCII (American Standard Code for Information Interchange) character encoding.

public sealed class ASCIIEncoding : Encoding

UTF8Encoding Class

Class

Represents the UTF-8 encoding of the Unicode character set.

public sealed class UTF8Encoding : Encoding

UnicodeEncoding Class

Class

Represents the UTF-16 little-endian encoding of the Unicode character set.

public sealed class UnicodeEncoding : Encoding