Android比较字符串是否为空(isEmpty)

时间:2022-06-16 19:52:53

经常需要判断一个字符串变量是否为空,今天特地做了个小小的测试

StringUtils.java:

package com.yx.equipment_collection.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;

/**
*
* 此类描述的是: String帮助类
*
* @author: CS YX
* @version:1.0
* @date:2014-10-21 下午2:47:08
*/
public class StringUtils {
private static final String TAG = "StringUtils";
private static int count = 100000000;

public static void checkEmpty1(String str) {
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
if (str == null || str.length() < 1) {
}
}
long end = System.currentTimeMillis();
Log.i(TAG, "checkEmpty1 --- " + (end - start));
}

@SuppressLint("NewApi")
public static void checkEmpty2(String str) {
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
if (str == null || str.isEmpty()) {
}
}
long end = System.currentTimeMillis();
Log.i(TAG, "checkEmpty2 --- " + (end - start));
}

public static void checkEmpty3(String str) {
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
if (str == null || str == "") {
}
}
long end = System.currentTimeMillis();
Log.i(TAG, "checkEmpty3 --- " + (end - start));
}

public static void checkEmpty4(String str) {
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
if (str == null || str.equals("")) {
}
}
long end = System.currentTimeMillis();
Log.i(TAG, "checkEmpty4 --- " + (end - start));

}

public static void checkEmpty5(String str) {
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
if (str == null || TextUtils.isEmpty(str)) {
}
}
long end = System.currentTimeMillis();
Log.i(TAG, "checkEmpty5 --- " + (end - start));

}

public static void checkEmpty11(String str) {
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
if (str != null && str.length() > 0) {
}
}
long end = System.currentTimeMillis();
Log.i(TAG, "checkEmpty11 --- " + (end - start));
}

@SuppressLint("NewApi")
public static void checkEmpty22(String str) {
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
if (str != null && !str.isEmpty()) {
}
}
long end = System.currentTimeMillis();
Log.i(TAG, "checkEmpty22 --- " + (end - start));
}

public static void checkEmpty33(String str) {
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
if (str != null && str != "") {
}
}
long end = System.currentTimeMillis();
Log.i(TAG, "checkEmpty33 --- " + (end - start));
}

public static void checkEmpty44(String str) {
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
if (str != null && !str.equals("")) {
}
}
long end = System.currentTimeMillis();
Log.i(TAG, "checkEmpty44 --- " + (end - start));

}

public static void checkEmpty55(String str) {
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
if (str != null && !TextUtils.isEmpty(str)) {
}
}
long end = System.currentTimeMillis();
Log.i(TAG, "checkEmpty55 --- " + (end - start));

}

}

测试为空如下:test

public void test() {
String str = "";
Log.i("test", "str=\"\"---");
StringUtils.checkEmpty1(str);
StringUtils.checkEmpty2(str);
StringUtils.checkEmpty3(str);
StringUtils.checkEmpty4(str);
StringUtils.checkEmpty5(str);
str = null;
Log.i("test", "str=null---");
StringUtils.checkEmpty1(str);
StringUtils.checkEmpty2(str);
StringUtils.checkEmpty3(str);
StringUtils.checkEmpty4(str);
StringUtils.checkEmpty5(str);
str = "null";
Log.i("test", "str=\"null\"---");
StringUtils.checkEmpty1(str);
StringUtils.checkEmpty2(str);
StringUtils.checkEmpty3(str);
StringUtils.checkEmpty4(str);
StringUtils.checkEmpty5(str);
str = new String();
Log.i("test", "str=new String()---");
StringUtils.checkEmpty1(str);
StringUtils.checkEmpty2(str);
StringUtils.checkEmpty3(str);
StringUtils.checkEmpty4(str);
StringUtils.checkEmpty5(str);

}

测试结果输入如下图:

Android比较字符串是否为空(isEmpty)

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

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

 测试非空如下:test

public void test1() {
String str = "";
Log.i("test", "str=\"\"---");
StringUtils.checkEmpty11(str);
StringUtils.checkEmpty22(str);
StringUtils.checkEmpty33(str);
StringUtils.checkEmpty44(str);
StringUtils.checkEmpty55(str);
str = null;
Log.i("test", "str=null---");
StringUtils.checkEmpty11(str);
StringUtils.checkEmpty22(str);
StringUtils.checkEmpty33(str);
StringUtils.checkEmpty44(str);
StringUtils.checkEmpty55(str);
str = "null";
Log.i("test", "str=\"null\"---");
StringUtils.checkEmpty11(str);
StringUtils.checkEmpty22(str);
StringUtils.checkEmpty33(str);
StringUtils.checkEmpty44(str);
StringUtils.checkEmpty55(str);
str = new String();
Log.i("test", "str=new String()---");
StringUtils.checkEmpty11(str);
StringUtils.checkEmpty22(str);
StringUtils.checkEmpty33(str);
StringUtils.checkEmpty44(str);
StringUtils.checkEmpty55(str);
}

测试结果如下图:

 Android比较字符串是否为空(isEmpty)

如上图所示,首先是方法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’:

    /**
* Returns true if the string is null or 0-length.
* @param str the string to be examined
* @return true if str is null or zero length
*/
public static boolean isEmpty(CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}

源码也是用.length()判断的,如果你觉得‘==’不靠谱,推荐使用.length()方法判断!

以上纯属个人见解......谢谢