比较可能为null的字符串的更好方法 [英] A better way to compare Strings which could be null
本文介绍了比较可能为null的字符串的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想知道是否有更好的方法来做到这一点。我觉得它可能效率低下。问题是出于DB的原因我需要比较有时可能为null的字符串。
I was just wondering if there is a better way to do this. i feel it might be inefficient. Problem is for DB reasons i need to compare strings which can sometimes be null or not.
public static boolean compareStrings(String str1, String str2){
if(str1 == null && str2 == null) return true;
if(str1 != null && str2 != null){
if(str1.equals(str2))
return true;
}
return false;
}
推荐答案
通常的习惯用法是这样的:
The usual idiom is this:
return (str1 == null ? str2 == null : str1.equals(str2));
这篇关于比较可能为null的字符串的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文