在C#中的字符串编辑的字符字符串、字符、编辑

由网友(可爱达人)分享简介:什么是编辑字符串中的字符在C#中最干净的方法?什么是C#相当于在C ++:的std ::字符串MyString的=潮;MyString的[0] =D;解决方案 于是决定什么时间,我觉得其中两个最经典的方法,还有一个我扔在psented为unre $ P $;这里是我发现(发布版本):ReplaceAtChars:86...

什么是编辑字符串中的字符在C#中最干净的方法?

什么是C#相当于在C ++:

 的std ::字符串MyString的=潮;
MyString的[0] =D;
 

解决方案

于是决定什么时间,我觉得其中两个最经典的方法,还有一个我扔在psented为unre $ P $;这里是我发现(发布版本):

 ReplaceAtChars:86ms
ReplaceAtSubstring:258ms
ReplaceAtStringBuilder:161ms 

显然,字符数组的方法是迄今为止最好的运行时优化。这实际上表明,目前的领先的的答案(StringBuilder的)很可能不是的最佳的答案。

这里是测试我使用的:

 类节目
{
    静态无效的主要(字串[] args)
    {
        Console.WriteLine(ReplaceAtChars:。+新秒表()时间(()=>中考.ReplaceAtChars(1,'E')ReplaceAtChars(3,T),1000000)+毫秒) ;
        Console.WriteLine(ReplaceAtSubstring:。+新秒表()时间(()=>中考.ReplaceAtSubstring(1,'E')ReplaceAtSubstring(3,T),1000000)+毫秒) ;
        Console.WriteLine(ReplaceAtStringBuilder:。+新秒表()时间(()=>中考.ReplaceAtStringBuilder(1,'E')ReplaceAtStringBuilder(3,T),1000000)+毫秒) ;
    }
}

公共静态类ReplaceAtExtensions
{
    公共静态字符串ReplaceAtChars(这串源,INT指数,字符替换)
    {
        变种临时= source.ToCharArray();
        临时[指数] =更换;
        返回新的String(临时);
    }

    公共静态字符串ReplaceAtStringBuilder(这串源,INT指数,字符替换)
    {
        VAR SB =新的StringBuilder(源);
        某人[指数] =更换;
        返回sb.ToString();
    }

    公共静态字符串ReplaceAtSubstring(这串源,INT指数,字符替换)
    {
        返回source.Substring(0,索引)+替换+ source.Substring(指数+ 1);
    }
}

公共静态类StopwatchExtensions
{
    公共静态长的时间(这秒表SW,行动行动,诠释迭代)
    {
        sw.Reset();
        sw.Start();
        的for(int i = 0; I<迭代;我++)
        {
            行动();
        }
        sw.Stop();

        返回sw.ElapsedMilliseconds;
    }
}
 
C语言 编写函数,将一个字符串中元音字母复制到另一个字符串

What's the cleanest way of editing the characters in a string in C#?

What's the C# equivalent of this in C++:

std::string myString = "boom";
myString[0] = "d";

解决方案

Decided to time what I felt where the two most canonical approaches, plus one I threw in as unrepresented; here's what I found (Release build):

ReplaceAtChars: 86ms
ReplaceAtSubstring: 258ms
ReplaceAtStringBuilder: 161ms

Clearly the Char array approach is by far best optimized by the runtime. Which actually suggests that the current leading answer (StringBuilder) is likely not the best answer.

And here was the test I used:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("ReplaceAtChars: " + new Stopwatch().Time(() => "test".ReplaceAtChars(1, 'E').ReplaceAtChars(3, 'T'), 1000000) + "ms");
        Console.WriteLine("ReplaceAtSubstring: " + new Stopwatch().Time(() => "test".ReplaceAtSubstring(1, 'E').ReplaceAtSubstring(3, 'T'), 1000000) + "ms");
        Console.WriteLine("ReplaceAtStringBuilder: " + new Stopwatch().Time(() => "test".ReplaceAtStringBuilder(1, 'E').ReplaceAtStringBuilder(3, 'T'), 1000000) + "ms");
    }
}

public static class ReplaceAtExtensions
{
    public static string ReplaceAtChars(this string source, int index, char replacement)
    {
        var temp = source.ToCharArray();
        temp[index] = replacement;
        return new String(temp);
    }

    public static string ReplaceAtStringBuilder(this string source, int index, char replacement)
    {
        var sb = new StringBuilder(source);
        sb[index] = replacement;
        return sb.ToString();
    }

    public static string ReplaceAtSubstring(this string source, int index, char replacement)
    {
        return source.Substring(0, index) + replacement + source.Substring(index + 1);
    }
}

public static class StopwatchExtensions
{
    public static long Time(this Stopwatch sw, Action action, int iterations)
    {
        sw.Reset();
        sw.Start();
        for (int i = 0; i < iterations; i++)
        {
            action();
        }
        sw.Stop();

        return sw.ElapsedMilliseconds;
    }
}

阅读全文

相关推荐

最新文章