通常情况下,我们需要去推断一个字符串变量是否为空,今天,我特意做了一个小测试

StringUtils.java:

  1. package com.yx.equipment_collection.utils;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.text.TextUtils;
  5. import android.util.Log;
  6.  
  7. /**
  8. *
  9. * 此类描写叙述的是: String帮助类
  10. *
  11. * @author: CS YX
  12. * @version:1.0
  13. * @date:2014-10-21 下午2:47:08
  14. */
  15. public class StringUtils {
  16. private static final String TAG = "StringUtils";
  17. private static int count = 100000000;
  18.  
  19. public static void checkEmpty1(String str) {
  20. long start = System.currentTimeMillis();
  21. for (int i = 0; i < count; i++) {
  22. if (str == null || str.length() < 1) {
  23. }
  24. }
  25. long end = System.currentTimeMillis();
  26. Log.i(TAG, "checkEmpty1 --- " + (end - start));
  27. }
  28.  
  29. @SuppressLint("NewApi")
  30. public static void checkEmpty2(String str) {
  31. long start = System.currentTimeMillis();
  32. for (int i = 0; i < count; i++) {
  33. if (str == null || str.isEmpty()) {
  34. }
  35. }
  36. long end = System.currentTimeMillis();
  37. Log.i(TAG, "checkEmpty2 --- " + (end - start));
  38. }
  39.  
  40. public static void checkEmpty3(String str) {
  41. long start = System.currentTimeMillis();
  42. for (int i = 0; i < count; i++) {
  43. if (str == null || str == "") {
  44. }
  45. }
  46. long end = System.currentTimeMillis();
  47. Log.i(TAG, "checkEmpty3 --- " + (end - start));
  48. }
  49.  
  50. public static void checkEmpty4(String str) {
  51. long start = System.currentTimeMillis();
  52. for (int i = 0; i < count; i++) {
  53. if (str == null || str.equals("")) {
  54. }
  55. }
  56. long end = System.currentTimeMillis();
  57. Log.i(TAG, "checkEmpty4 --- " + (end - start));
  58.  
  59. }
  60.  
  61. public static void checkEmpty5(String str) {
  62. long start = System.currentTimeMillis();
  63. for (int i = 0; i < count; i++) {
  64. if (str == null || TextUtils.isEmpty(str)) {
  65. }
  66. }
  67. long end = System.currentTimeMillis();
  68. Log.i(TAG, "checkEmpty5 --- " + (end - start));
  69.  
  70. }
  71.  
  72. public static void checkEmpty11(String str) {
  73. long start = System.currentTimeMillis();
  74. for (int i = 0; i < count; i++) {
  75. if (str != null && str.length() > 0) {
  76. }
  77. }
  78. long end = System.currentTimeMillis();
  79. Log.i(TAG, "checkEmpty11 --- " + (end - start));
  80. }
  81.  
  82. @SuppressLint("NewApi")
  83. public static void checkEmpty22(String str) {
  84. long start = System.currentTimeMillis();
  85. for (int i = 0; i < count; i++) {
  86. if (str != null && !str.isEmpty()) {
  87. }
  88. }
  89. long end = System.currentTimeMillis();
  90. Log.i(TAG, "checkEmpty22 --- " + (end - start));
  91. }
  92.  
  93. public static void checkEmpty33(String str) {
  94. long start = System.currentTimeMillis();
  95. for (int i = 0; i < count; i++) {
  96. if (str != null && str != "") {
  97. }
  98. }
  99. long end = System.currentTimeMillis();
  100. Log.i(TAG, "checkEmpty33 --- " + (end - start));
  101. }
  102.  
  103. public static void checkEmpty44(String str) {
  104. long start = System.currentTimeMillis();
  105. for (int i = 0; i < count; i++) {
  106. if (str != null && !str.equals("")) {
  107. }
  108. }
  109. long end = System.currentTimeMillis();
  110. Log.i(TAG, "checkEmpty44 --- " + (end - start));
  111.  
  112. }
  113.  
  114. public static void checkEmpty55(String str) {
  115. long start = System.currentTimeMillis();
  116. for (int i = 0; i < count; i++) {
  117. if (str != null && !TextUtils.isEmpty(str)) {
  118. }
  119. }
  120. long end = System.currentTimeMillis();
  121. Log.i(TAG, "checkEmpty55 --- " + (end - start));
  122.  
  123. }
  124.  
  125. }

測试为空例如以下:test

  1. public void test() {
  2. String str = "";
  3. Log.i("test", "str=\"\"---");
  4. StringUtils.checkEmpty1(str);
  5. StringUtils.checkEmpty2(str);
  6. StringUtils.checkEmpty3(str);
  7. StringUtils.checkEmpty4(str);
  8. StringUtils.checkEmpty5(str);
  9. str = null;
  10. Log.i("test", "str=null---");
  11. StringUtils.checkEmpty1(str);
  12. StringUtils.checkEmpty2(str);
  13. StringUtils.checkEmpty3(str);
  14. StringUtils.checkEmpty4(str);
  15. StringUtils.checkEmpty5(str);
  16. str = "null";
  17. Log.i("test", "str=\"null\"---");
  18. StringUtils.checkEmpty1(str);
  19. StringUtils.checkEmpty2(str);
  20. StringUtils.checkEmpty3(str);
  21. StringUtils.checkEmpty4(str);
  22. StringUtils.checkEmpty5(str);
  23. str = new String();
  24. Log.i("test", "str=new String()---");
  25. StringUtils.checkEmpty1(str);
  26. StringUtils.checkEmpty2(str);
  27. StringUtils.checkEmpty3(str);
  28. StringUtils.checkEmpty4(str);
  29. StringUtils.checkEmpty5(str);
  30.  
  31. }

測试结果输入例如以下图:

由此图能够看出方法3(str == "")用时是最少的;其次就是方法1(str.length() < 1)和方法2(str.isEmpty()) ;

方法4(str.equals(""))耗时较多;方法5(TextUtils.isEmpty(str))最耗时

測试非空例如以下:test

  1. public void test1() {
  2. String str = "";
  3. Log.i("test", "str=\"\"---");
  4. StringUtils.checkEmpty11(str);
  5. StringUtils.checkEmpty22(str);
  6. StringUtils.checkEmpty33(str);
  7. StringUtils.checkEmpty44(str);
  8. StringUtils.checkEmpty55(str);
  9. str = null;
  10. Log.i("test", "str=null---");
  11. StringUtils.checkEmpty11(str);
  12. StringUtils.checkEmpty22(str);
  13. StringUtils.checkEmpty33(str);
  14. StringUtils.checkEmpty44(str);
  15. StringUtils.checkEmpty55(str);
  16. str = "null";
  17. Log.i("test", "str=\"null\"---");
  18. StringUtils.checkEmpty11(str);
  19. StringUtils.checkEmpty22(str);
  20. StringUtils.checkEmpty33(str);
  21. StringUtils.checkEmpty44(str);
  22. StringUtils.checkEmpty55(str);
  23. str = new String();
  24. Log.i("test", "str=new String()---");
  25. StringUtils.checkEmpty11(str);
  26. StringUtils.checkEmpty22(str);
  27. StringUtils.checkEmpty33(str);
  28. StringUtils.checkEmpty44(str);
  29. StringUtils.checkEmpty55(str);
  30. }

測试结果例如以下图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ194aW5nXw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">

如上图所看到的,首先是方法33(str != null && str != "")比較占优势。方法11(str != null && str.length() > 0)和方法22(str != null && !str.isEmpty())整体来说。不相上下;

方法44(str != null && !str.equals(str != null && !TextUtils.isEmpty(str)))较耗时;方法55()还是最耗时

也有人说,用‘==’推断字符串不准确,应该用‘equals’,个人认为String一般都是直接=定义,想必没有几个人定义一个字符串会new出来吧。

为什么TextUtils.isEmpty()耗时最多,查看源代码原来isEmpty()已经推断了‘==null’:

  1. /**
  2. * Returns true if the string is null or 0-length.
  3. * @param str the string to be examined
  4. * @return true if str is null or zero length
  5. */
  6. public static boolean isEmpty(CharSequence str) {
  7. if (str == null || str.length() == 0)
  8. return true;
  9. else
  10. return false;
  11. }

源代码还用于.length()推断,假设你想‘==’不靠谱。推荐使用.length()对于推理方法!

不是单纯的个人意见......谢谢




Android比较字符串是空的(isEmpty)的更多相关文章

  1. C#判断字符串为空

    string str = null; if (string.IsNullOrWhiteSpace(str)) { MessageBox.Show("字符串为null"); } if ...

  2. C#判断字符串为空的几种方法和效率判断

    C#判断字符串为空的几种方法和效率判断 string定义 1.1 string str1="":会定义指针(栈),并在内存里划一块值为空的存储空间(堆),指针指向这个空间.1.2 ...

  3. java 字符串为空问题

    java 字符串为空问题 String testStr = null; System.out.println(testStr); if (testStr == null) { System.out.p ...

  4. Jquery取小数后边2位,N位;jQuery去掉字符串首尾空字符串

    function fix(num, N) { , N); return Math.round(num * base) / base; } 实例,取小数后边两位 var yhmoney2 = fix(1 ...

  5. Shell脚本中字符串判空:使用-z 字符串长度为0时,为真,-n字符串长度不为0,为真。这两个都不靠谱【转】

    最近发现使用  -z   和  -n  来判断字符串判空,或不空时,很不靠谱. 使用下面的方法最可靠: if [ "x${value}" == "x" ]    ...

  6. shell脚本----if(数字条件,字符串条件,字符串为空)

    二元比较操作符,比较变量或者比较数字. 注意数字与字符串的区别. 1.整数比较  -eq 等于,如:if [ "$a" -eq "$b" ] -ne 不等于,如 ...

  7. C# 判断字符串为空(长度为0),或者是null(没有new)

    string str = null; if (string.IsNullOrWhiteSpace(str)) { MessageBox.Show("字符串为null"); } ) ...

  8. C# 判断字符串为空的4种方法及效率

    在程序开发过程中,少不了要处理字符串,并且常常要判断字符串是否为空,通常有哪些判断方法,以及不同方法的效率又怎么样? 在 C# 中,通常有三种判断字符串是否为空的方法,下面分别探讨. 1.str.Le ...

  9. Linux shell 判断字符串为空等常用命令

    1.判断字符串为空 if [ -z "$str" ]; then echo "empty string" fi 2.判断文件是否存在 if [ -f /home ...

随机推荐

  1. JS Call()与Apply()

    JS Call()与Apply() ECMAScript规范给所有函数都定义了Call()与apply()两个方法,call与apply的第一个参数都是需要调用的函数对象,在函数体内这个参数就是thi ...

  2. 怎样制作一个相似Tiny Wings的游戏 Cocos2d-x 2.1.4

    在第一篇<怎样使用CCRenderTexture创建动态纹理>基础上,添加�创建动态山丘,原文<How To Create A Game Like Tiny Wings with C ...

  3. ASM丢失disk header导致ORA-15032、ORA-15040、ORA-15042 Diskgroup无法mount

    SQL> select * from v$version; BANNER --------------------------– Oracle Database 11g Enterprise E ...

  4. Qt4_VS10 程序打包发布

    源地址:http://www.2cto.com/kf/201306/217205.html 目录结构如下: ---------------------------------------------- ...

  5. axure制作圆形组件——axure制作技巧

    Axure本身是没有直接提供圆形组件的,所以很多朋友在微博上问,如何使用axure制作圆形,难道都要找美工-- Axure没有提供圆形组件,但是它提供了一个万能组件--矩形组件,只要有矩形组件,我们就 ...

  6. html,JavaScript调用winfrom方法

    ---恢复内容开始--- 目的: 在动画上面添加点击事件,通过JavaScript调用winfrom方法 1.创建一个页面 using System; using System.Collections ...

  7. Spring MVC 的json问题(406 Not Acceptable)

    原因 : 就是程序转换JSON失败. 在pom.xml 加上 <dependency> <groupId>com.fasterxml.jackson.core</grou ...

  8. 1.0.3-学习Opencv与MFC混合编程之---打开本地摄像头

    源代码:http://download.csdn.net/detail/nuptboyzhb/3961643 版本1.0.3新增内容 打开摄像头 Ø 新建菜单项,Learning OpenCV——&g ...

  9. sed 技巧一例:特定位置插入

    通过一例子熟悉 sed 的运用 下面命令是在修改 ~/fs/install/nzos.conf 文件, 并在 env 第一次出现的地方再添加一行 env LXC_EXTRA_PORT=5556 sed ...

  10. access数据库:怎么直接从access里把数据里同样的文字替换成空字符&quot;&quot;

    access数据库:怎么直接从access里把数据里同样的文字替换成空字符"" 搜所到文字后,替换的项里写"",就是了.一定要是英文的""