String
字符串对象一旦被初始化就不会被改变
字符串常量池
String s = "abc"; //字符串常量池
String s = new String("abc");//在堆内存中
字符串== 比较地址值
String类中的equals复写了Object,比较字符串内容
String构造函数
常见方法
获取
转换 字符串切割
split
trim
判断
比较
intern方法 对字符串池进行操作
字符串数组排序
获取字符串次数
private static int getStringcounts(String ss, String key) {
int counts = 0;
int index = 0;
while((index=ss.indexOf(key))!= -1){
index += key.length();
ss = ss.substring(index);
counts++;
}
return counts;
}
查找最大子字符串
public static String maxSubString(String s1,String s2){
String max = null;
String min = null;
max=(s1.length()>s2.length())?s1:s2;
min=(max.equals(s1))?s2:s1; for (int i = 0; i <min.length() ; i++) {
for (int j =0,k=min.length()-i;k!=min.length()+1;j++,k++) {
String sub = min.substring(j,k);
if(max.contains(sub))
return sub;
}
}
return null;
}
去除两端空白 模拟trim()
private static String getStringTrim(String s1) {
int start = 0,end = s1.length()-1;
while(start<=end && ' ' == s1.charAt(start)){
start++;
}
while(start<=end && ' ' == s1.charAt(end)){
end--;
}
return s1.substring(start,end+1);
}
StringBuffer
append insert
添加、删除、查找、修改
//清空缓冲区
sb.delete(0.sb.lenght())
sb = new StringBuffer();
setLength();
StringBuilder
单线程,提高效率,不安全