有人可以建议如何将字符串拆分为数组列表,条件是数组列表的每个字符串大小应小于 1 MB。
我在字符串变量中提供了字符串值。我需要遍历字符串值,然后检查字符串大小应始终 > 1MB,如果大小超过 1MB,则拆分字符串数据并将其存储在数组列表的子字符串中。
有人可以建议我如何在下面的代码中实现它吗:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System;
class Program
{
public static void Main()
{
String[][] TextFile = new String[5][] { { "Mike", "Amy" }, { "Mary", "Albert" } } ;
for (int i = 0; i < TextFile.Length; i++)
{
TextFile[i] = new String[i + 1];
}
for (int i = 0; i < TextFile.Length; i++)
{
Console.WriteLine("Length of row {0} is {1}", i, TextFile[i].Length);
}
}
}
最佳答案
您尝试做的事情有点尴尬。如何获得每个字符的字节数,然后相应地计算字符串长度。
System.Text.ASCIIEncoding.Unicode.GetByteCount(string);
System.Text.ASCIIEncoding.ASCII.GetByteCount(string);
另一种进行调查的方法,如果您真的想尝试这种方法。
Encoding.Default.GetBytes("Hello");
关于c# - 拆分大小小于 1MB 的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31747086/