如何知道字符串的大小(以字节为单位)?

问题描述 投票:0回答:4

我想知道我是否可以知道 C# 中

string
的字节长度,有人知道吗?

c# .net string size byte
4个回答
180
投票

您可以使用 ASCII 等编码通过使用

System.Text.Encoding
类来获取每个字节的字符。

或者试试这个

  System.Text.ASCIIEncoding.Unicode.GetByteCount(string);
  System.Text.ASCIIEncoding.ASCII.GetByteCount(string);

121
投票

来自MSDN

String
对象是表示字符串的
System.Char
对象的顺序集合。

所以你可以使用这个:

var howManyBytes = yourString.Length * sizeof(Char);

29
投票
System.Text.ASCIIEncoding.Unicode.GetByteCount(yourString);

或者

System.Text.ASCIIEncoding.ASCII.GetByteCount(yourString);

11
投票

string
将占用多少字节取决于您选择的编码(或者在您不知情的情况下在后台自动选择)。此示例代码显示了差异:

using System;
using System.Text;

static void Main()
{
    Encoding testedEncodings = new[]
    {
        Encoding.ASCII,   // Note that '🡪' cannot be encoded in ASCII, data loss will occur
        Encoding.UTF8,    // This should always be your choice nowadays
        Encoding.Unicode, // This is UTF-16. It is used by .NET to store your strings in RAM when the application is running, but this isn't useful information unless you're trying to manipulate bytes in RAM
        Encoding.UTF32
    };

    string text = "a🡪";

    Console.WriteLine($"Tested string: {text}");
    Console.WriteLine($"String length: {text.Length}");
    Console.WriteLine();

    PrintTableHeader("Encoding", "Bytes", "Decoded string");

    foreach (var encoding in testedEncodings)
    {
        byte[] bytes = encoding.GetBytes(text);
        string decodedString = encoding.GetString(bytes);

        PrintTableRow(
            encoding.EncodingName,
            $"{bytes.Length} ({string.Join(' ', bytes)})",
            decodedString);
    }
}

static void PrintTableHeader(params string[] values)
{
    PrintTableRow(values);
    Console.WriteLine(new string('-', 60));
}

static void PrintTableRow(params string[] values)
{
    Console.WriteLine("{0,-16} | {1,-24} | {2}", values);
}

输出:

Tested string: a🡪
String length: 3

Encoding         | Bytes                    | Decoded string
------------------------------------------------------------
US-ASCII         | 3 (97 63 63)             | a??
Unicode (UTF-8)  | 5 (97 240 159 161 170)   | a🡪
Unicode          | 6 (97 0 62 216 106 220)  | a🡪
Unicode (UTF-32) | 8 (97 0 0 0 106 248 1 0) | a🡪
© www.soinside.com 2019 - 2024. All rights reserved.