package test;
public class Test {
/*数组有length属性,没有length()方法,如:
int[] a; a.lengh; //返回a的长度。数组下表是length-1
String只有length()方法,没有length属性,如:String s;s.length();*///返回s的长度。
//基本操作
/*public static void main(String[] args) {
String s="abcdefgabc";
System.out.println("字符串的长度:"+s.length());
System.out.println(s.charAt(5));//0-5 返回第6位,5是索引值
//int indexOf(int ch)
System.out.println(s.indexOf('e'));//0-4
System.out.println(s.lastIndexOf('a'));//0-7
}*/
//字符串的转换操作
/*public static void main(String[] args) {
String str="abcd";
System.out.println("转换结果:");
char[] charArray=str.toCharArray();
for(int i=0;i<=charArray.length-1;i++){
if(i!=charArray.length-1){
System.out.print(charArray[i]+",");
}else{
System.out.println(charArray[i]);
}
}
System.out.println("将int转换为String:"+String.valueOf(12));
//String valueOf(int i)
System.out.println(str.toUpperCase());
}*/
//字符串的空格和去空格操作
/*public static void main(String[] args) {
String str="abcd";
System.out.println("将 ab替换成cn.it=========="+str.replace("ab","cn.it"));
String str1="h e l l o";
System.out.println(str1.trim());//去除字符串首尾的空格
System.out.println("去除所有的空格:"+str1.replace(" ", ""));//(old,new)
}*/
/*//字符串的判断操作
public static void main(String[] args) {
String s1="String";
String s2="Str";
System.out.println("判断是否以Str开头"+s1.startsWith("Str"));//prefix
System.out.println("判断是否以ng结尾:"+s1.endsWith("ng"));
System.out.println("判断是否包含字符串tri:"+s1.contains("tri"));
System.out.println(s1.isEmpty());//判断是否为空
System.out.println(s1.equals(s2));
System.out.println(s1==s2);
//equals 用于比较两个字符串是否相等
//==用于比较字符串对象地址是否相同,显然是s1 s2是不同的对象
}*/
/*//字符串的截取和分割
public static void main(String[] args) {
String str="abc-edf-zxc";
System.out.println("从第五个字符串截取到末尾:"+str.substring(4));//从第4+1个开始
System.out.println("从第五个字符截取到第6个字符的结果:"+str.substring(4, 6));//从4+1个开始到 第6个
System.out.println("分割后的字符串数组中的元素一次为:");
//String[] split(String regex)//regex正则表达式,根据regex将字符串分割为若干子串
//返回的是一个字符串数组
String[] strArray=str.split("-");
for(int i=0;i<strArray.length;i++){
System.out.println(strArray[i]);
}
}*/
//StringBuffer类
//常用的方法 append(char c)
// insert(int offset,String str)
// deleteCharAt(int index)
// delete(int start,int end)
// replace(start,end,String s)
// void setCharAt(int index,char ch)//修改指定位置index出的字符
// reverse()将字符串反过来
/*public static void main(String[] args) {
//1.添加
StringBuffer sb=new StringBuffer();
sb.append("xxxaaabbb");
System.out.println("old sb===="+sb);
sb.insert(2, "123");//在第3个ch处开始插入
System.out.println("new sb==="+sb);
//2.删除
StringBuffer sb1=new StringBuffer("abcdefg");
sb1.delete(1, 5);
System.out.println(sb1);//结果是afg 0 12345 6
sb1.deleteCharAt(2);
System.out.println(sb1);//result=af g被删除
sb.delete(0, sb1.length());//清空sb1
System.out.println(sb1);
//3.修改 替换
StringBuffer sb2=new StringBuffer("abcdef");
sb2.setCharAt(1, 'p');
System.out.println(sb2);//b变成p apcdef
sb2.replace(1, 3, "qqw");
System.out.println(sb2);//aqqwdef 0 12 3 bc--qqw
}*/
}
String和StringBuffer 的区别
1. 内容和长度 Stringbuffer可变 String不可变
2.+符号 StringBuffer不能用 sb1+sb2编译报错; String可以 str1+str2
3.equals() String覆盖了 Object类的equals()方法 ;StringBuffer没有