Android比较字符串是空的(isEmpty)
通常情况下,我们需要去推断一个字符串变量是否为空,今天,我特意做了一个小测试
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); }
測试结果输入例如以下图:
由此图能够看出方法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);
}
測试结果例如以下图:
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’:
/**
* 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()对于推理方法!
不是单纯的个人意见......谢谢
Android比较字符串是空的(isEmpty)的更多相关文章
- C#判断字符串为空
string str = null; if (string.IsNullOrWhiteSpace(str)) { MessageBox.Show("字符串为null"); } if ...
- C#判断字符串为空的几种方法和效率判断
C#判断字符串为空的几种方法和效率判断 string定义 1.1 string str1="":会定义指针(栈),并在内存里划一块值为空的存储空间(堆),指针指向这个空间.1.2 ...
- java 字符串为空问题
java 字符串为空问题 String testStr = null; System.out.println(testStr); if (testStr == null) { System.out.p ...
- Jquery取小数后边2位,N位;jQuery去掉字符串首尾空字符串
function fix(num, N) { , N); return Math.round(num * base) / base; } 实例,取小数后边两位 var yhmoney2 = fix(1 ...
- Shell脚本中字符串判空:使用-z 字符串长度为0时,为真,-n字符串长度不为0,为真。这两个都不靠谱【转】
最近发现使用 -z 和 -n 来判断字符串判空,或不空时,很不靠谱. 使用下面的方法最可靠: if [ "x${value}" == "x" ] ...
- shell脚本----if(数字条件,字符串条件,字符串为空)
二元比较操作符,比较变量或者比较数字. 注意数字与字符串的区别. 1.整数比较 -eq 等于,如:if [ "$a" -eq "$b" ] -ne 不等于,如 ...
- C# 判断字符串为空(长度为0),或者是null(没有new)
string str = null; if (string.IsNullOrWhiteSpace(str)) { MessageBox.Show("字符串为null"); } ) ...
- C# 判断字符串为空的4种方法及效率
在程序开发过程中,少不了要处理字符串,并且常常要判断字符串是否为空,通常有哪些判断方法,以及不同方法的效率又怎么样? 在 C# 中,通常有三种判断字符串是否为空的方法,下面分别探讨. 1.str.Le ...
- Linux shell 判断字符串为空等常用命令
1.判断字符串为空 if [ -z "$str" ]; then echo "empty string" fi 2.判断文件是否存在 if [ -f /home ...
随机推荐
- XSS漏洞解决方案之一:过滤器
一:web.xml文件 <!-- 解决xss漏洞 --> <filter> <filter-name>xssFilter</filter-name> ...
- Why Python?
Python is object-oriented Structure supports such concepts as polymorphism , operation overloading , ...
- 《Swift Programming Language 》——Swift中怎样使用继承(Inheritance)
一个类能够继承(inherit)还有一个类的方法(methods),属性(property)和其他特性.当一个类继承其他类时,继承类叫子类(subclass),被继承类叫超类(或父类,supercla ...
- Android实时获取音量(单位:分贝)
基础知识 度量声音强度,大家最熟悉的单位就是分贝(decibel,缩写为dB).这是一个无纲量的相对单位,计算公式如下: 分子是测量值的声压,分母是参考值的声压(20微帕,人类所能听到的最小声压).因 ...
- Ubuntu下SVN命令行递归加入文件夹文件(免去一个一个的加入 --force)
因为在Linux下一直没有找到好的svn工具(类似于TortiseSVN的).当然eSVN这些也不错,但就是使用上认为还不是很习惯.终于还是选择了svn原始的命令行工具来进行版本号控制操作. 命令行的 ...
- hive udaf 用maven打包运行create temporary function 时报错
用maven打包写好的jar,在放到hive中作暂时函数时报错. 错误信息例如以下: hive> create temporary function maxvalue as "com. ...
- 初识Dubbo 系列之4-Dubbo 依赖
依赖 必需依赖 JDK1.5+ 理论上Dubbo能够仅仅依赖JDK,不依赖于不论什么三方库执行,仅仅需配置使用JDK相关实现策略. 缺省依赖 通过mvn dependency:tree > de ...
- 在Mac OS X苹果lion系统上制作USB启动盘
本文翻译自:http://evan.borgstrom.ca/post/1314205955/osx-bootable-usb-from-iso 我也就不按照原文上一句句的翻译了,只说几个比较重要的步 ...
- 临界段CCriticalSection的使用
类CCriticalSection的对象表示一个“临界区”,它是一个用于同步的对象,同一时刻仅仅同意一个线程存取资源或代码区.临界区在控制一次仅仅有一个线程改动数据或其他的控制资源时很实用.比如,在链 ...
- C语言深度解剖读书笔记(6.函数的核心)
对于本节的函数内容其实就没什么难点了,但是对于函数这节又涉及到了顺序点的问题,我觉得可以还是忽略吧. 本节知识点: 1.函数中的顺序点:f(k,k++); 这样的问题大多跟编译器有关,不要去刻意追求 ...