It is often necessary to determine whether a string variable is empty, and a small test was made today.
Stringutils.java:
Package Com.yx.equipment_collection.utils;import Android.annotation.suppresslint;import android.text.TextUtils; Import android.util.log;/** * * This class describes: String helper class * * @author: CS YX * @version: 1.0 * @date: 2014-10-21 pm 2:47:08 */publi C class StringUtils {private static final String TAG = "StringUtils";p rivate static int count = 100000000;public static VO ID 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++) {i F (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++) {i F (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++) {i F (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++) {i F (str! = NULL &&!) Textutils.isempty (str)) {}}long end = System.currenttimemillis (); LOG.I (TAG, "checkEmpty55---" + (End-start));}}
The test is empty as follows: 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);}
Test results input such as:
The figure shows that Method 3 (str = = ") is the least used, followed by Method 1 (Str.length () < 1) and Method 2 (Str.isempty ());
Method 4 (Str.equals ("")) takes more time; Method 5 (Textutils.isempty (str)) is the most time-consuming
The test is non-empty as follows: 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);}
Test results such as:
As shown, first is the method (str! = NULL && str! = "") compared to the advantage; method One (str! = null && str.length () > 0) and method (str! = null & &!str.isempty ()) overall, comparable;
Method (str! = NULL &&!str.equals (str! = NULL &&!) Textutils.isempty (str)) is time consuming; Method 55 () is the most time-consuming
Some people say, "= =" To determine the string is not accurate, should use ' equals ', personally think that string is generally direct = definition, presumably not a few people define a string will be new out.
Why Textutils.isempty () the most time-consuming, view Source original IsEmpty () has judged the ' ==null ':
/** * Returns True if the string is null or 0-length. * @param str The string to being examined * @return true if STR is null or zero length */Public Static Boolean I Sempty (charsequence str) { if (str = = NULL | | str.length () = = 0) return true; else return false; }
Source code is also used in. Length (), if you think ' = = ' is not reliable, recommended to use. Length () method to judge!
The above is purely personal opinion ... Thank you
Whether the Android comparison string is empty (IsEmpty)