Split string less than 1 MB
Can someone suggest how to split a string into a list of arrays with the condition that the size of each string in the arraylist must be less than 1MB.
I have string values represented in a string variable. I need to go through string values and then check that the size of the string should always be> 1MB, and if the size is more than 1MB then split the string data and store it in substrings of array lists.
Can someone suggest how I can implement it in my below code:
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);
}
}
}
source to share
What you are trying to do is a little awkward. Once you can get the number of bytes per character, then calculate the length of the string accordingly.
System.Text.ASCIIEncoding.Unicode.GetByteCount(string);
System.Text.ASCIIEncoding.ASCII.GetByteCount(string);
Another way to do your research if you really want to try this approach.
Encoding.Default.GetBytes("Hello");
source to share
CharithJ's answer is good, but only for completion.
If you have a non-content size requirement, I suppose you don't need to read / modify the details separately. If so, you don't need them on the string, but the [] byte is enough. This way you don't need to calculate the size of the char encoding (or even know the encoding). You can convert string to byte [] (byte-byte)
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
Then you can use this SO question to split it into parts with less than 1,048,576 (1MB).
source to share