- public class Demo {
- public static void main(String[] args) {
- String s1 = "";
- String s2 = " ";
- String s3 = null;
- System.out.println(s1.isEmpty());//true
- System.out.println(s2.isEmpty());//false
- // System.out.println(s3.isEmpty());//运行时异常:NullPointerException
-
- String s4 = "HelloWorld";
- System.out.println("是否以Hello开头:" + s4.startsWith("Hello"));//true
- System.out.println("是否以hello开头:" + s4.startsWith("hello"));//false
- System.out.println("是否以空字符串开头:" + s4.startsWith(""));//true
- System.out.println("是否以H开头:" + s4.startsWith("H"));//true
- // System.out.println("是否以null开头:" + s4.startsWith(null));//空指针异常
-
- System.out.println("是否以World结尾:" + s4.endsWith("World"));//true
- System.out.println("是否以world结尾:" + s4.endsWith("world"));//false
- System.out.println("是否以空字符串结尾:" + s4.endsWith(""));//true
- System.out.println("------------------------------------------");
- //boolean contains(string)
- String s5 = "HelloWorld";
- System.out.println("是否包含Hello:" + s5.contains("Hello"));//true
- System.out.println("是否包含hello:" + s5.contains("hello"));//false
- System.out.println("是否包含llo:" + s5.contains("llo"));//true
- System.out.println("是否包含空字符串:" + s5.contains(""));//true
-
- System.out.println("------------------------------------------");
- String s6 = "jfl;sjafkdjavajfkoewutjavajfkdofewjava";
- System.out.println("java第一次出现的位置:" + s6.indexOf("java"));
- System.out.println("Java第一次出现的位置:" + s6.indexOf("Java"));
- System.out.println("java最后一次出现的位置:"+ s6.lastIndexOf("java"));
-
- System.out.println("------------------------------------------");
- String s7 = "我爱Java";
- char[] chArray =s7.toCharArray();
- System.out.println(chArray);//字符
-
- System.out.println("------------------------------------------");
- String s8 = "abcd";
- byte[] byteArray = s8.getBytes();
- System.out.println(byteArray);//地址
- for(int i = 0 ;i < byteArray.length ; i++){
- System.out.println(byteArray[i]);
- }
- }
- }
复制代码
|
|