System.Text Namespace
Provides fundamental classes that support the encoding and decoding of characters, including the ability to access all the .NET character encodings.
System.Text.Encoding
Class
Represents a character encoding. Encoding objects are immutable.
-
static Encoding ASCII { get; }
Gets an encoding for the ASCII (7-bit) character set.
Encoding.ASCII.GetString(bytes)
-
static Encoding UTF8 { get; }
Gets an encoding for UTF-8, which is a variable-length encoding.
Encoding.UTF8.GetBytes(text)
-
static Encoding Unicode { get; }
Gets an encoding for UTF-16 Little Endian.
Encoding.Unicode.GetString(bytes)
-
static Encoding UTF32 { get; }
Gets an encoding for UTF-32, which is a fixed-length encoding.
Encoding.UTF32.GetBytes(text)
-
static Encoding Default { get; }
Gets the default encoding for the current computer.
Encoding.Default.EncodingName
-
int GetByteCount(char[] chars)
Returns the number of bytes into which the specified array of characters will be encoded.
Encoding.UTF8.GetByteCount(myChars)
-
byte[] GetBytes(char[] chars)
Encodes all the characters in the specified character array into a sequence of bytes.
Encoding.ASCII.GetBytes(charArray)
-
string GetString(byte[] bytes)
Decodes all the bytes in the specified byte array into a string.
Encoding.UTF8.GetString(utf8Bytes)
System.Text.StringBuilder
Class
Represents a mutable sequence of characters.
-
StringBuilder()
Initializes a new instance of the StringBuilder class.
var sb = new StringBuilder();
-
StringBuilder(int capacity)
Initializes a new instance of the StringBuilder class that has an initial capacity.
var sb = new StringBuilder(100);
-
StringBuilder Append(string value)
Appends the string representation of the value parameter to this instance.
sb.Append("Hello");
-
StringBuilder AppendLine(string value)
Appends a copy of the specified string followed by the default line terminator to the end of this instance.
sb.AppendLine("World");
-
string ToString()
Converts to String the value of this instance.
string finalString = sb.ToString();
System.Text.Decoder
Class
Represents a character decoder.
-
int GetCharCount(byte[] bytes, int index, int count)
Calculates the number of characters produced by decoding the specified portion of the byte array.
decoder.GetCharCount(buffer, 0, bytesRead)
-
int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
Decodes a range of bytes from a byte array into a character array.
decoder.GetChars(byteData, 0, len, charArray, 0)
System.Text.Encoder
Class
Represents a character encoder.
-
int GetByteCount(char[] chars, int index, int count, bool flush)
Calculates the number of bytes produced by encoding the specified portion of the character array.
encoder.GetByteCount(charBuffer, 0, charsCount, true)
-
int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)
Encodes a range of characters from a character array into a byte array.
encoder.GetBytes(chars, 0, numChars, buffer, 0, false)