判断String是否为空的小技巧

 

              //比较 方法一,大多数人都这么比较

		String param = config.getInitParameter("log-level");
		if (param != null && !"".equals(param)) {
			final Level level = Log.getLevel(param);
			if (level != null) Log.lookup("org.zkoss").setLevel(level);
			else log.error("Unknown log-level: "+param);
		}
             //比较 方法二,(而我研究的源码基本都这么比较)
              String param = config.getInitParameter("log-level");
		if (param != null && param.length() > 0) {
			final Level level = Log.getLevel(param);
			if (level != null) Log.lookup("org.zkoss").setLevel(level);
			else log.error("Unknown log-level: "+param);
		}

 // 下面我们看一下equals,length,isEmpty的源码

  //String中equals方法内部代码

  public boolean equals(Object anObject) {
	if (this == anObject) {
	    return true;
	}
	if (anObject instanceof String) {
	    String anotherString = (String)anObject;
	    int n = count;
	    if (n == anotherString.count) {
		char v1[] = value;
		char v2[] = anotherString.value;
		int i = offset;
		int j = anotherString.offset;
		while (n-- != 0) {
		    if (v1[i++] != v2[j++])
			return false;
		}
		return true;
	    }
	}
	return false;
    }

   //String.length()的源码

 /**
     * Returns the length of this string.
     * The length is equal to the number of <a href="Character.html#unicode">Unicode
     * code units</a> in the string.
     *
     * @return  the length of the sequence of characters represented by this
     *          object.
     */
    public int length() {
        return count;
    }
    //String.isEmpty的源码
  /**
     * Returns <tt>true</tt> if, and only if, {@link #length()} is <tt>0</tt>.
     *
     * @return <tt>true</tt> if {@link #length()} is <tt>0</tt>, otherwise
     * <tt>false</tt>
     *
     * @since 1.6
     */
    public boolean isEmpty() {
	return count == 0;
    }






 

仅从代码多少以及逻辑判断来看,length,isEmpty都非常简单,对于一般比较判断来说,

 

首先我猜测:length,isEmpty是相差无几的,equals方法就望尘莫及了,

 

 

 

下面做一个测试,证明我们的猜测

 

测试代码如下

 

 

private static final String sunflower = "sunflower";
	private static final int COUNT= 1000000000;

	public static void main(String[] args) {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		System.out.println("开始计算...\n");

		Long begin = System.currentTimeMillis();
		for (int j = 0; j < COUNT; j++) {
			testEmptyByLength(sunflower);
		}
		calculateDuration("length test", begin);

		begin = System.currentTimeMillis();

		for (int j = 0; j < COUNT; j++) {
			testEmptyByEmpty(sunflower);
		}

		calculateDuration("empty test", begin);

		begin = System.currentTimeMillis();

		for (int j = 0; j < COUNT; j++) {
			testEmptyByEquals(sunflower);
		}
		calculateDuration("equals test", begin);
	}

	public static void calculateDuration(String method, long begin) {
		System.out.println(method + "\t:"
				+ (System.currentTimeMillis() - begin) / 1000);
	}

	public static boolean testEmptyByLength(String str) {
		return str == null || str.length() < 1;
	}

	/**
	 * empty方法是jdk1.6添加的方法,如果是早期版本不兼容
	 * 
	 * @param str
	 * @return
	 */
	public static boolean testEmptyByEmpty(String str) {
		return str == null || str.isEmpty();
	}

	public static boolean testEmptyByEquals(String str) {
		return "".equals(str);
	}
 

 

输出结果:

 

 

 

 

开始计算(s)

 

length test :0

empty test :0

equlas test :8


 

 

 

结论:

 

String.isEmpty()方法是jdk1.6中添加的,其方法体和length()方法一样,从性能和兼容性,length()更好,

 

所以如果对字符串非严格判断是否为空,建议使用String.length>0判断,如果严格判断是否全部为" "字符,就要自己写或者用common lang StringUtils.isBlank了

 

isBlank方法代码补充:

 

 

  /**
     * <p>Checks if a String is whitespace, empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * </pre>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is null, empty or whitespace
     * @since 2.0
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }
 

 

 

注:本人不是性能主义者,而是追求更好的方法!!

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下代码在曾经每个模块设计和调试时存在问题的思考,存在什么问题及解决方法,以及算法的改进设想#include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> #include <iterator> using namespace std; typedef istream_iterator<string> string_input; void welcome() { cout << "******************* 变位词查找系统*********************\n" << "在词典中找出给定的字符串的所有变位词" << endl; } void readDict(vector<string> & dictionary) { cout << "首先,请输入词典的文件名称:" << endl; string dictionary_name; cin >> dictionary_name; ifstream ifs(dictionary_name.c_str()); if (!ifs.is_open()) { cerr << "异常:文件"<< dictionary_name << "没有找到 " << endl; exit(1); } cout << "词典读入中 ..." << flush; copy(string_input(ifs), string_input(), back_inserter(dictionary)); sort(dictionary.begin(),dictionary.end()); cout << "词典包含有 " << dictionary.size() << " 个单词\n\n"; ifs.close(); } void analyseAnagram(const vector<string> & dictionary) { cout << "请输入单词(或任意字母序列)" << endl; for (string_input p(cin); p != string_input(); ++p) { cout << "查找输入单词的变位词中..." << endl; string word = *p; sort(word.begin(), word.end()); bool found_one = false; do { if (binary_search(dictionary.begin(), dictionary.end(), word)) { cout << " " << word ; found_one = true; } } while (next_permutation(word.begin(), word.end())); if (!found_one) cout << " 抱歉,没有找到变位词\n"; cout << "\n请输入下一个单词 " << "(或输入Ctrl+Z终止程序 ) \n" << endl; } } int main() { welcome(); vector<string> dictionary; readDict(dictionary); analyseAnagram(dictionary); system("pause"); return 0; }
06-08

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值