如何加快从字符串中删除文本的方法?

Edw*_*uay 6 c# performance text

我编写了以下方法来从字符串中删除括号的命名空间.

我愿把这作为快速地.

有没有办法加快以下代码?

using System;

namespace TestRemoveFast
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] tests = {
            "{http://company.com/Services/Types}ModifiedAt",
            "{http://company.com/Services/Types}CreatedAt"
                             };

            foreach (var test in tests)
            {
                Console.WriteLine(Clean(test));
            }

            Console.ReadLine();
        }

        static string Clean(string line)
        {
            int pos = line.IndexOf('}');
            if (pos > 0)
                return line.Substring(pos + 1, line.Length - pos - 1);
            else
                return line;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dyn*_*ard 3

您可以尝试并行性,因为它看起来不需要同步处理。使用 PLINQ 的并行 foreach 就可以解决问题。

但如果你等不及 VS2010 正式发布,你可以尝试Emre Aydinceren 的 Poor Man's Parallel.ForEach Iterator