Pergunta

I have this function. The visual studio profile marked the line with string.Format as hot and were i spend much of my time.

How can i write this loop more efficiently?

    public string EscapeNoPredicate(string sz)
    {
        var s = new StringBuilder(sz);

        s.Replace(sepStr, sepStr + sepStr);
        foreach (char v in IllegalChars)
        {
            string s2 = string.Format("{0}{1:X2}", seperator, (Int16)v);
            s.Replace(v.ToString(), s2);
        }
        return s.ToString();
    }
Foi útil?

Solução

Instead of calculating s2s foreach v each time this method is called; you can store them precalculated. Of course I am assuming IllegalChars and seperator remains same.

Outras dicas

  • In a string.format you can put objects, so (Int16)v is not needed. You can supply "v"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top