首页 > 代码库 > Java Day 15

Java Day 15

String
 字符串对象一旦被初始化就不会被改变
 
 字符串常量池
 
 String s = "abc"; //字符串常量池
 String s = new String("abc");//在堆内存中
 字符串== 比较地址值
 String类中的equals复写了Object,比较字符串内容
 
String构造函数

常见方法
 获取
 转换 字符串切割
  split
  trim
 判断
 比较
 intern方法 对字符串池进行操作

字符串数组排序
获取字符串次数

 1     private static int getStringcounts(String ss, String key) { 2         int counts = 0; 3         int index = 0; 4         while((index=ss.indexOf(key))!= -1){ 5             index += key.length(); 6             ss = ss.substring(index); 7             counts++; 8         } 9         return counts;10     }


查找最大子字符串

 1     public static String maxSubString(String s1,String s2){ 2         String max = null; 3         String min = null; 4         max=(s1.length()>s2.length())?s1:s2; 5         min=(max.equals(s1))?s2:s1; 6  7         for (int i = 0; i <min.length() ; i++) { 8             for (int j =0,k=min.length()-i;k!=min.length()+1;j++,k++) { 9                 String sub = min.substring(j,k);10                 if(max.contains(sub))11                     return sub;12             }13         }14         return null;15     }


去除两端空白 模拟trim()

 1     private static String getStringTrim(String s1) { 2         int start = 0,end = s1.length()-1; 3         while(start<=end && ‘ ‘ == s1.charAt(start)){ 4             start++; 5         } 6         while(start<=end && ‘ ‘ == s1.charAt(end)){ 7             end--; 8         } 9         return s1.substring(start,end+1);10     }

 

StringBuffer
 append insert
 添加、删除、查找、修改
 //清空缓冲区
 sb.delete(0.sb.lenght())
 sb = new StringBuffer();

 setLength();
 
StringBuilder
 单线程,提高效率,不安全

Java Day 15