Şaf*_*Gür 12 .net c# arrays string char
我有一个非常大的char数组,我需要将其转换为字符串才能在其上使用Regex.
但是OutOfMemoryException当我将它传递给字符串构造函数时,它是如此之大.
我知道字符串是不可变的,因此不应该指定它的底层字符集,但我需要一种方法来使用正则表达式而不复制整个事物.
我如何获得该阵列?
StreamReader.我知道要读取的内容的起始位置和长度,Read并且ReadBlock方法需要我提供char[]缓冲区.所以这是我想知道的事情:
我认为最好的选择是将多个 char[] 块读取到与某个维度重叠的单个字符串中。这样,您就能够对各个块执行正则表达式,并且重叠将使您能够确保块中的“中断”不会破坏搜索模式。以伪代码的方式:
int chunkSize = 100000;
int overLap = 2000;
for(int i = 0; i < myCharArray.length; i += chunkSize - overlap)
{
// Grab your array chunk into a partial string
// By having your iteration slightly smaller than
// your chunk size you guarantee not to miss any
// character groupings. You just need to make sure
// your overlap is sufficient to cover the expression
string chunk = new String(myCharArray.Skip(i).Take(chunkSize).ToArray());
// run your regex
}
Run Code Online (Sandbox Code Playgroud)