文章类型: ANDROID
关键词: android,判断,字符串,是否,为空,最优,方法
内容摘要: android 判断字符串是否为空的最优方法

android 判断字符串是否为空的最优方法

2019/10/23 16:04:33    来源:apple    阅读:

 在android 的开发中经常会使用判断字符串是否为空,虽然现在智能手机的运行速度越来越高,但为了使应用更加的流畅,我们应该保证在编写程序时使用较好的方法来提高效率。一下为四种方法的运行时间的比较:

public class TestEmptyString {
   String  s   = "";
   long    n   = 10000000;
   
   private void function1() {
       long startTime = System.currentTimeMillis();
       for (long i = 0; i < n; i++) {
           if (s == null || s.equals(""))
               ;
       }
       
       long endTime = System.currentTimeMillis();
       System.out.println("function 1 use time: " + (endTime - startTime)
       + "ms");
   }
   
   private void function2() {
       long startTime = System.currentTimeMillis();
       for (long i = 0; i < n; i++) {
           if (s == null || s.length() <= 0)
               ;
       }
       
       long endTime = System.currentTimeMillis();
       System.out.println("function 2 use time: " + (endTime - startTime)
       + "ms");
   }
   
   private void function3() {
       long startTime = System.currentTimeMillis();
       for (long i = 0; i < n; i++) {
           if (s == null || s.isEmpty())
               ;
       }
       
       long endTime = System.currentTimeMillis();
       System.out.println("function 3 use time: " + (endTime - startTime)
       + "ms");
   }
   
   private void function4() {
       long startTime = System.currentTimeMillis();
       for (long i = 0; i < n; i++) {
           if (s == null || s == "")
               ;
       }
       
       long endTime = System.currentTimeMillis();
       System.out.println("function 4 use time: " + (endTime - startTime)
       + "ms");
   }
   
   public static void main(String[] args) {
       TestEmptyString test = new TestEmptyString();
       test.function1();
       test.function2();
       test.function3();
       test.function4();
}

    

    方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低.


 方法二: 比较字符串长度, 效率高, 是我知道的最好一个方法.


    方法三: Java SE 6.0 才开始提供的办法, 效率和方法二基本上相等, 但出于兼容性考虑, 推荐使用方法二或方法四.


    方法四: 这是种最直观,简便的方法,而且效率也非常的高,与方法二、三的效率差不多


  以下代码在我机器上的运行结果: (机器性能不一, 仅供参考)


  function 1 use time: 140ms


  function 2 use time: 47ms


  function 3 use time: 47ms


  function 4 use time: 47ms



  注意:s == null 是有必要存在的.

  如果 String 类型为 null, 而去进行 equals(String) 或 length() 等操作会抛出java.lang.NullPointerException.

  并且s==null 的顺序必须出现在前面.不然同样会抛出java.lang.NullPointerException.


  如下代码:

  Java代码

    String str= = null;
  if(str=.equals("") || str= == null){//会抛出异常
      System.out.println("success");
  }
  // "".equales(str);后置确保不会遇null报错。


↑ 上一篇文章:Android主线程不能访问网络异常解决办法 关键词:Android,主线程,不能,访问,网络,异常,解决,办法 发布日期:2019/10/22 15:55:48
↓ 下一篇文章:Error:(2, 0) Plugin with id 'com.github.dcendents.android-maven' not found解决办法 关键词:Plugin,with,id,,com,github.d.. 发布日期:2019/10/31 14:54:37
相关文章:
VC++如何判断字符串是否有全为数字 关键词:VC,判断,字符串,全为,数字 发布日期:2017-11-28 16:26
VC++判断文件或文件夹是否存在 关键词:VC++,判断,文件,文件夹,是否,存在 发布日期:2017-11-22 10:46
js判断是否为空(二) 关键词:js,javascript,判断,空,Null,NULL,null,字符串,实例 发布日期:2015-07-29 14:01
相关目录:.NETANDROIDJAVA
我要评论
正在加载评论信息......