开发者

Fix string for JavaScript in C#

I have a function that fixed non-printable characters in C# for JavaScript. But it works very slow! How to increase speed of this function?

private static string JsStringFixNonPrintable(string Source)
    {
        string Result = "";
        for (int Position = 0; Position < Source.Length; ++Position)
        {
            int i = Position;
            var CharCat = char.GetUnicodeCategory(Source, i);
            if (Char.IsWhiteSpace(Source[i]) ||
                CharCat == System.Globalization.UnicodeCategory.LineSeparator 开发者_如何学C||
                CharCat == System.Globalization.UnicodeCategory.SpaceSeparator) { Result += " "; continue; }
            if (Char.IsControl(Source[i]) && Source[i] != 10 && Source[i] != 13) continue;
            Result += Source[i];
        }
        return Result;
    }


I have recoded your snippet of code using StringBuilder class, with predefined buffer size... that is much faster than your sample.

    private static string JsStringFixNonPrintable(string Source)
    {
        StringBuilder builder = new StringBuilder(Source.Length); // predefine size to be the same as input
        for (int it = 0; it < Source.Length; ++it)
        {
            var ch = Source[it];
            var CharCat = char.GetUnicodeCategory(Source, it);
            if (Char.IsWhiteSpace(ch) ||
                CharCat == System.Globalization.UnicodeCategory.LineSeparator ||
                CharCat == System.Globalization.UnicodeCategory.SpaceSeparator) { builder.Append(' '); continue; }
            if (Char.IsControl(ch) && ch != 10 && ch != 13) continue;
            builder.Append(ch);
        }
        return builder.ToString();
    }


Instead of concatenating to the string, try using System.Text.StringBuilder which internally maintains a character buffer and does not create a new object every time you append.

Example:

StringBuilder sb = new StringBuilder();
sb.Append('a');
sb.Append('b');
sb.Append('c');
string result = sb.ToString();
Console.WriteLine(result); // prints 'abc'


Use Stringbuilder

http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx

and replace characters in-place, that should speed up things

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜