Java String类

1.substring() 方法返回字符串的子字符串。

public String substring(int beginIndex)

public String substring(int beginIndex, int endIndex)

1 class Main {
2     public static void main(String[] args) {
3         String str = new String("this my practice");
4         System.out.print("返回值:");
5         System.out.println(str.substring(4));
6         System.out.print("返回值:");
7         System.out.println(str.substring(4,8));
8     }
9 }

2.isEmpty() 方法用于判断字符串是否为空

如果字符串为空返回 true,否则返回 false。

字符串通过 length() 方法计算字符串长度,如果返回 0,即为空字符串。

1 class Main {
2     public static void main(String[] args) {
3     String str1 = "djlsakd";
4     String str2 = "";
5     System.out.println(str1.isEmpty());
6     System.out.println(str2.isEmpty());
7     }
8 }

 

posted on 2023-10-16 21:20  messing  阅读(16)  评论(0)    收藏  举报

导航