当前位置:  首页>> 技术小册>> Java语言基础9-常用API和常见算法

1.8.1 那些是空字符串

  • 空字符串的长度是 0 。

  • 示例:

  1. package com.github.string.demo25;
  2. /**
  3. * 空字符串
  4. *
  5. * @author maxiaoke.com
  6. * @version 1.0
  7. *
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. String s1 = "";
  12. String s2 = new String();
  13. String s3 = new String("");
  14. System.out.println(s1);
  15. System.out.println(s2);
  16. System.out.println(s3);
  17. }
  18. }

1.8.2 如何判断某个字符串是否为空字符串?

  • 示例:
  1. package com.github.string.demo26;
  2. /**
  3. * @author maxiaoke.com
  4. * @version 1.0
  5. *
  6. */
  7. public class Test {
  8. public static void main(String[] args) {
  9. String str = "";
  10. if ("".equals(str)) {
  11. System.out.println("是空字符串");
  12. }
  13. }
  14. }
  • 示例:
  1. package com.github.string.demo27;
  2. /**
  3. * @author maxiaoke.com
  4. * @version 1.0
  5. *
  6. */
  7. public class Test {
  8. public static void main(String[] args) {
  9. String str = "";
  10. if (null != str && str.isEmpty()) {
  11. System.out.println("是空字符串");
  12. }
  13. }
  14. }
  • 示例:
  1. package com.github.string.demo28;
  2. /**
  3. * @author maxiaoke.com
  4. * @version 1.0
  5. *
  6. */
  7. public class Test {
  8. public static void main(String[] args) {
  9. String str = "";
  10. if(null != str && str.equals("")){
  11. System.out.println("是空字符串");
  12. }
  13. }
  14. }
  • 示例:
  1. package com.github.string.demo29;
  2. /**
  3. * @author maxiaoke.com
  4. * @version 1.0
  5. *
  6. */
  7. public class Test {
  8. public static void main(String[] args) {
  9. String str = "";
  10. if(null != str && str.length() ==0){
  11. System.out.println("是空字符串");
  12. }
  13. }
  14. }

该分类下的相关小册推荐: