示例代码:
public class Demo2 { public static void main(String[] args) { String string = "jav a"; // 1. public int length():返回字符串的长度 System.out.println(string.length()); // 2. public char charArt(int index):根据下标获取字符 System.out.println(string.charAt(string.length()-1)); // 3. publiuc boolean contains(String str):判断当前字符串中是否包含str。 System.out.println(string.contains("java")); System.out.println(string.contains("ja")); System.out.println("----------------------------------"); // 4. public char[] toCharArray():将字符串转换成数组 System.out.println(Arrays.toString(string.toCharArray())); // 5. public int indexOf(String str):查找str首次出现的下标,存在返回该下标,不存在返回-1。 System.out.println(string.indexOf("a")); // 6. public int lastIndexOf(String str):查找字符串在当前字符串中最后一次出现的下标索引。 System.out.println(string.lastIndexOf("a")); System.out.println("----------------------------------"); String s = " QiuDajiang "; // 7. public String trim():去掉字符串前后的空格 System.out.println(s.trim()); // 8. publiuc String toUpperCase():将小写转换成大写 大写转为小写 System.out.println(s.toUpperCase()); System.out.println(s.toLowerCase()); // 9. public boolean endWith(String str):判断字符串是否以str结尾/开头 System.out.println(s.endsWith("jiang")); System.out.println(s.startsWith(" ")); System.out.println("----------------------------------"); String s1 = "qiu da jiang zui shuai le,mei ren bi ta shuai"; // 10. public String replace(char oldChar,char newChar):将旧字符串替换成新字符串 System.out.println(s1.replace("i","x")); // 11. public String[] split(String str):根据str拆分 System.out.println(Arrays.toString(s1.split("[ ,]+"))); System.out.println("----------------------------------"); //补充 equals() 和 compareTo() 方法 compareTo()用于比较大小 String s2 = "hello"; String s3 = "HELLO"; System.out.println(s2.equals(s3)); System.out.println(s2.equalsIgnoreCase(s3)); //忽略大小写 String s4 = "abc"; //a -> 97 String s5 = "xyzasd"; //x -> 120 System.out.println(s4.compareTo(s5)); //97-120 = -23 s4-s5 String s6 = "abc"; String s7 = "abcxyz"; System.out.println(s6.compareTo(s7)); //-3 比较长度 } }
public class Demo2 { public static void main(String[] args) { StringBuffer stringBuffer = new StringBuffer(); //1.append(); 追加 stringBuffer.append("java"); System.out.println(stringBuffer.toString()); stringBuffer.append("first"); System.out.println(stringBuffer.toString()); //2.insert(); 添加 stringBuffer.insert(0,"我在最前面"); System.out.println(stringBuffer.toString()); //3.replace(); 替换 stringBuffer.replace(0,5,"前面"); System.out.println(stringBuffer.toString()); //4.delete(); 删除 stringBuffer.delete(0,1); System.out.println(stringBuffer.toString()); //清空 stringBuffer.delete(0,stringBuffer.length()); System.out.println(stringBuffer.toString()); } }
StringBuilder(效率比String高,比StringBuffer高):
示例代码:
public class Demo2 { public static void main(String[] args) { //String效率低 // String string = " "; // long start = System.currentTimeMillis(); // for (int i = 0; i <99999 ; i++) { // string+=i; // } // long end = System.currentTimeMillis(); // System.out.println(end-start); //18293 //StringBuilrder效率高 StringBuilder stringBuilder = new StringBuilder(); long start = System.currentTimeMillis(); for (int i = 0; i <99999 ; i++) { stringBuilder.append(i); } long end = System.currentTimeMillis(); System.out.println(end-start); //8 } }