Android比較字串是否為空白(isEmpty)

來源:互聯網
上載者:User

標籤:string

經常需要判斷一個字串變數是否為空白,今天特地做了個小小的測試
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);}

測試結果如:

 

如所示,首先是方法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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.