相关疑难解决方法(0)

如何快速替换数组中的字符

我在XML文件上使用XML文本阅读器,该文件可能包含对读者无效的字符.我最初的想法是创建我自己的流阅读器版本并清除坏角色,但它严重减慢了我的程序.

public class ClensingStream : StreamReader
{
        private static char[] badChars = { '\x00', '\x09', '\x0A', '\x10' };
    //snip
        public override int Read(char[] buffer, int index, int count)
        {
            var tmp = base.Read(buffer, index, count);

            for (int i = 0; i < buffer.Length; ++i)
            {
                //check the element in the buffer to see if it is one of the bad characters.
                if(badChars.Contains(buffer[i]))
                    buffer[i] = ' ';
            }

            return tmp;
        }
}
Run Code Online (Sandbox Code Playgroud)

根据我的探查器,代码花了88%的时间用于if(badChars.Contains(buffer[i]))正确的方法,所以我没有造成可怕的缓慢?

c# optimization

4
推荐指数
1
解决办法
3273
查看次数

标签 统计

c# ×1

optimization ×1