【三种青年】之判断字符串内容是否为空
普通青年:public boolean isEmpty(String s) {
if ("".equals(s)) {
return true;
}
return false;
}
文艺青年:
public boolean isEmpty(String s) {
if (s.length() <= 0) {
return true;
}
return false;
}
2B青年:
public boolean isEmpty(String s) {--------------------编程问答--------------------
if (s == null) {
return true;
}
return false;
}
/**--------------------编程问答-------------------- 字符串是否为空 用isEmpty判断不太好吧 …… --------------------编程问答-------------------- 普通+2B:
* 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;
}
--------------------编程问答-------------------- null 不是 空串。
public boolean isEmpty(String s) {
if (s==null||"".equals(s)) {
return true;
}
return false;
}
所以普通青年,2B青年的代码都错。 --------------------编程问答-------------------- 晕晕晕,我在胡言乱语 --------------------编程问答-------------------- 现在“2B”两个字很流行啊 --------------------编程问答-------------------- 空字符串 String str = "";
空串 String str = null; --------------------编程问答-------------------- null不能用equals()方法判断的,""和null不同哦。 --------------------编程问答-------------------- 啥意思?? 什么青年? --------------------编程问答-------------------- 我说的是字符串内容为空,不是空串=,= --------------------编程问答-------------------- s="";不是null是空白值
public boolean isEmpty(String s) {
if (s==null && s.length==0) {
return true;
}
return false;
}
这样是什么青年? --------------------编程问答-------------------- public static boolean isNull(String target) {
return (target == null || target.length() < 1) ? true : false;
}
LZ说说这个是什么青年 呵呵 --------------------编程问答--------------------
public static boolean isNull(String target) {
return (target == null || target.length() < 1);
}
那你说这个算什么青年 呵呵 --------------------编程问答-------------------- 那用三目运算符的是什么青年 --------------------编程问答-------------------- 这都是什么青年… --------------------编程问答-------------------- public static boolean PTisEmpty(String s) {
if ("".equals(s)) {
return true;
}
return false;
}
public static boolean WYisEmpty(String s) {
if (s.length() <= 0) {
return true;
}
return false;
}
public static boolean ERBisEmpty(String s) {
if (s == null) {
return true;
}
return false;
}
public static boolean isEmpty2B(String s) {
if (s==null||"".equals(s)) {
return true;
}
return false;
}
public static boolean isEmpty3B(String s) {
if (s==null && s.length()==0) {
return true;
}
return false;
}
public static boolean isNull(String target) {
return (target == null || target.length() < 1) ? true : false;
}
public static void main(String[] args) {
System.out.println(PTisEmpty(" "));
System.out.println(WYisEmpty(" "));
System.out.println(ERBisEmpty(" "));
System.out.println(isEmpty2B(" "));
System.out.println(isNull(" "));
System.out.println(isEmpty3B(" "));
}
}
果断全部false,BBB --------------------编程问答-------------------- 写程序,都是青年!都很年轻啊 呵呵 --------------------编程问答-------------------- 空串 -- "" --已经初始化
空 -- null --没有初始化
isEmpty()是判断是否为空呢 还是判断空串 ? 存在二义性啊 --------------------编程问答-------------------- if判断,再return true,false,你们真有才!!
直接return if里面的条件不行啊
补充:Java , Java SE