内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/810442/whats-the-most-efficient-way-to-determine-whether-an-untrimmed-string-is-empty
我有一个字符串可能在其周围有空格字符,我想检查它是否基本上是空的.
有很多方法可以做到这一点:
1 if (myString.Trim().Length == 0) 2 if (myString.Trim() == "") 3 if (myString.Trim().Equals("")) 4 if (myString.Trim() == String.Empty) 5 if (myString.Trim().Equals(String.Empty))
我知道这通常会是过早优化的一个明显的例子,但我很好奇,有一个机会,这样做会有足够的表现影响.
那么哪些是最有效的方法?
有没有什么更好的方法我没有想到?
编辑:访客对此问题的注释:
>对这个问题进行了一些非常详细的调查,特别是Andy和Jon Skeet.
>如果您在寻找某些东西时偶然遇到这个问题,那么至少阅读Andy和Jon的帖子是非常值得的.
似乎有一些非常有效的方法,最有效率取决于我需要处理的字符串的内容.
如果我无法预测字符串(我不能在我的情况下),Jon的IsEmptyOrWhiteSpace方法似乎通常更快.
感谢您的投入.我要选择安迪的答案是“正确的”,因为他应该为他所投入的努力提高声誉,而乔恩已经拥有十亿的声誉.
编辑:新测试:
Test orders: x. Test name Ticks: xxxxx //Empty String Ticks: xxxxx //two space Ticks: xxxxx //single letter Ticks: xxxxx //single letter with space Ticks: xxxxx //long string Ticks: xxxxx //long string with space 1. if (myString.Trim().Length == 0) ticks: 4121800 ticks: 7523992 ticks: 17655496 ticks: 29312608 ticks: 17302880 ticks: 38160224 2. if (myString.Trim() == "") ticks: 4862312 ticks: 8436560 ticks: 21833776 ticks: 32822200 ticks: 21655224 ticks: 42358016 3. if (myString.Trim().Equals("")) ticks: 5358744 ticks: 9336728 ticks: 18807512 ticks: 30340392 ticks: 18598608 ticks: 39978008 4. if (myString.Trim() == String.Empty) ticks: 4848368 ticks: 8306312 ticks: 21552736 ticks: 32081168 ticks: 21486048 ticks: 41667608 5. if (myString.Trim().Equals(String.Empty)) ticks: 5372720 ticks: 9263696 ticks: 18677728 ticks: 29634320 ticks: 18551904 ticks: 40183768 6. if (IsEmptyOrWhitespace(myString)) //See John Skeet's Post for algorithm ticks: 6597776 ticks: 9988304 ticks: 7855664 ticks: 7826296 ticks: 7885200 ticks: 7872776 7. is (string.IsNullOrEmpty(myString.Trim()) //Cloud's suggestion ticks: 4302232 ticks: 10200344 ticks: 18425416 ticks: 29490544 ticks: 17800136 ticks: 38161368
并使用代码:
public void Main() { string res = string.Empty; for (int j = 0; j <= 5; j++) { string myString = ""; switch (j) { case 0: myString = ""; break; case 1: myString = " "; break; case 2: myString = "x"; break; case 3: myString = "x "; break; case 4: myString = "this is a long string for testing triming empty things."; break; case 5: myString = "this is a long string for testing triming empty things. "; break; } bool result = false; Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i <= 100000; i++) { result = myString.Trim().Length == 0; } sw.Stop(); res += "ticks: " + sw.ElapsedTicks + Environment.NewLine; } Console.ReadKey(); //break point here to get the results }
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/810442/whats-the-most-efficient-way-to-determine-whether-an-untrimmed-string-is-empty
以上所述就是小编给大家介绍的《在C#中确定未修剪的字符串是否为空,最有效的方式是什么?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 如何修剪BERT达到加速目的?理论与实现
- 查找一个字符串中最长不含重复字符的子字符串,计算该最长子字符串的长度
- (三)C语言之字符串与字符串函数
- 算法笔记字符串处理问题H:编排字符串(2064)
- 如何在JavaScript中检查字符串是否包含子字符串?
- 字符串、字符处理总结
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Practical Algorithms for Programmers
Andrew Binstock、John Rex / Addison-Wesley Professional / 1995-06-29 / USD 39.99
Most algorithm books today are either academic textbooks or rehashes of the same tired set of algorithms. Practical Algorithms for Programmers is the first book to give complete code implementations o......一起来看看 《Practical Algorithms for Programmers》 这本书的介绍吧!