|
| 1 | +package String; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +public class StringMethodTest { |
| 6 | + @Test |
| 7 | + public void test4(){ |
| 8 | + String str1 = "helloworld"; |
| 9 | + System.out.println(str1.replace('h', 'H')); |
| 10 | + } |
| 11 | + |
| 12 | + |
| 13 | + @Test |
| 14 | + public void test3(){ |
| 15 | + String str1 = "helloworld"; |
| 16 | + System.out.println(str1.endsWith("ld")); |
| 17 | + System.out.println(str1.startsWith("hh")); |
| 18 | + //记录指定字符串的内容以及开始的位置 |
| 19 | + System.out.println(str1.startsWith("ll", 2)); |
| 20 | + |
| 21 | + System.out.println(str1.contains("wo")); |
| 22 | + //返回字符串的角标,如果没有则返回-1 |
| 23 | + System.out.println(str1.indexOf("lo")); |
| 24 | + System.out.println(str1.lastIndexOf("or")); |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + @Test |
| 30 | + public void test2(){ |
| 31 | + String s1 = "HelloWorld"; |
| 32 | + String s2 = "helloworld"; |
| 33 | + System.out.println(s1.equals(s2)); |
| 34 | + System.out.println(s1.equalsIgnoreCase(s2)); |
| 35 | + |
| 36 | + String s3 = "abc"; |
| 37 | + String s4 = s3.concat("def"); |
| 38 | + System.out.println(s4); |
| 39 | + |
| 40 | + String s5 = "abc"; |
| 41 | + String s6 = "abe"; |
| 42 | + //逐个比较两个字符串,返回值数据类型为int |
| 43 | + System.out.println(s5.compareTo(s6));//用于通讯录排序 |
| 44 | + |
| 45 | + String s7 = "helloworld"; |
| 46 | + System.out.println(s7.substring(3)); |
| 47 | + System.out.println(s7.substring(2, 4)); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + @Test |
| 52 | + public void test1(){ |
| 53 | + String s1 = "HelloWorld"; |
| 54 | + System.out.println(s1.length()); |
| 55 | + System.out.println(s1.charAt(0)); |
| 56 | + System.out.println(s1.isEmpty()); |
| 57 | + |
| 58 | + String s2 = s1.toLowerCase(); |
| 59 | + System.out.println(s2); |
| 60 | + |
| 61 | + String s3 = " hello w orld "; |
| 62 | + //去除首尾的空格 |
| 63 | + String s4 = s3.trim(); |
| 64 | + System.out.println(s4); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments