java8_api_字符串处理
字符串处理1
字符串处理2
string的常用方法
package java_20180209_api_string; public class StringDemo2 { public static void main(String[] args) { String s="abcdefcdefcdef"; System.out.println(s.charAt(2)); // String s1=""; String s1=new String(); System.out.println(s1.isEmpty()); System.out.println(s1==""); System.out.println(s1.equals("")); System.out.println("".equals(s1)); System.out.println(s.indexOf("cd",-3)); System.out.println(s.lastIndexOf("cd")); System.out.println(s.substring(4, 7)); System.out.println(" bce ".trim().length()); System.out.println(s.replace('c', 'C')); System.out.println(s.replace("cd", "XX")); System.out.println(s.startsWith("dab")); System.out.println(s.endsWith("ef")); String[] result=s.split("de"); for (String string : result) { System.out.println(string); } String result1=String.join("|", "java","is","cool"); System.out.println(result1); System.out.println(String.valueOf(new char[]{'1','2','3'},1,2)); char[] result2= {'a','a','a','d'}; System.out.println(String.valueOf(result2, 1, 1)); System.out.println("abc".compareTo("abcd")); } }
字符串处理3
StringBuffer与StringBuilder
使用+拼接String的内部优化
区别:String不可变,StringBuffer可变
stringBuilder的常用方法
使用+拼接String的内部优化
String str="x"+"y"+12.56;
其实被内部转换为String str=new StringBuilder().append("x").append("y").append(12.56).toString();
package java_20180209_api_string; public class StringDemo3 { public static void main(String[] args) { StringBuilder builder=new StringBuilder(); StringBuilder b=builder.append(11111).append("abcdefghi").append(1.2); System.out.println(b); System.out.println(b.length()+","+b.capacity()); System.out.println(b.insert(2, "xxx")); } }