1.8.1 那些是空字符串
空字符串的长度是 0 。
示例:
package com.github.string.demo25;
/**
* 空字符串
*
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
String s1 = "";
String s2 = new String();
String s3 = new String("");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
1.8.2 如何判断某个字符串是否为空字符串?
package com.github.string.demo26;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
String str = "";
if ("".equals(str)) {
System.out.println("是空字符串");
}
}
}
package com.github.string.demo27;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
String str = "";
if (null != str && str.isEmpty()) {
System.out.println("是空字符串");
}
}
}
package com.github.string.demo28;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
String str = "";
if(null != str && str.equals("")){
System.out.println("是空字符串");
}
}
}
package com.github.string.demo29;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
String str = "";
if(null != str && str.length() ==0){
System.out.println("是空字符串");
}
}
}