String:不可改变,没有缓存,没加锁
StringBuffer:可以改变,有缓存,加锁
StringBuilder:可以改变,没有缓存,不加锁,处理速度比StringBuffer快,但不安全
package com.fyb.oop.Test4;
public class Test {
public static void main(String args[]) {
//定义字符串
String s="今夜阳光明媚!";//不能被改变
String s1 = new String("线程");
System.out.println(s1);
char[] cs = {'s','d','f','s'};
char[] cs1 = {97,65,98,66};
String s2 = new String(cs1);
System.out.println(s2);
//
String s1="今夜阳光明媚!";//储存在常量池中
String s2="今夜阳光明媚!";//已经存在,s2直接指向s1储存在常量池中的
System.out.println(s1.equals(s2));//相同
String s1 = new String("线程");//s1空间开在堆中,里面存的常量池中的地址
String s2 = new String("线程");//s2空间开在堆中,里面存的常量池中的地址
System.out.println(s1.equals(s2));//常量池中的地址相同
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
String s1="今夜阳光明媚!";
// System.out.println(s1.contains("阳光"));//看字符串中是否包含
System.out.println(s1.substring(1,4));//截取字符串“夜阳光”,最后一个不包含,要加一
System.out.println(s1.substring(1));//截取字符串,从夜到最后
String s = "asdfghj";
// System.out.println(s.toUpperCase());//转大写
s = s.toUpperCase();
System.out.println(s);
System.out.println(s.toLowerCase());//转小写
// String s1="今夜-阳光-明媚!";
String[] ss = s1.split("-");//转数组
for (String s:ss){
System.out.println(s);
}
// System.out.println(s1.charAt(4));//截取字符
char[] cs =new char[s1.length()];//转字符数组
for (int i=0;i<s1.length();i++){
cs[i] = s1.charAt(i);
}
// char[] cs = s1.toCharArray();//转字符数组
String s1 = "abcd";
String s2 = "abf";
int i = s1.compareTo(s2);//比较相差多少
System.out.println(i);//-3
String s1="今abc夜-阳光-明媚!";
System.out.println(s1.codePointCount(1,5));//取值
System.out.println(s1.concat("123"));//追加
System.out.println(s1.compareToIgnoreCase("asdAS"));//忽略大小写
System.out.println(s1.endsWith("-明媚!"));//是否以...结尾
System.out.println(s1.startsWith("今abc夜-"));//是否以...开头
// String s1="今夜阳光明媚";
byte[] s = s1.getBytes();//转换成字节
System.out.println(s.length);
byte[] s =s1.getBytes("GBk");//转换成GDK
System.out.println(s.length);
String s1="今夜a阳97光a明媚";
System.out.println(s1.indexOf(97));//返回...下标,两个一样返回第一个
System.out.println(s1.indexOf("97"));//返回...下标,第一个的
System.out.println(s1.indexOf(97,3));//返回...的下标,从3开始找
System.out.println(s1.indexOf("97",6));//返回...的下标,从6开始找
String s1=" 今夜a阳97光a明媚 ";
s1.isEmpty();//是否为空
System.out.println(s1.replace('a','b'));//替换
System.out.println(s1.trim());//去前后空格
System.out.println(s1.lastIndexOf("a"));//从右往左查,返回第一个的位置
StringBuffer s1 = new StringBuffer();//只能存16个字符
StringBuffer s2 = new StringBuffer(32);//能存32个字符
StringBuffer s3 = new StringBuffer("abc");//能存abc+16=19个字符
System.out.println(s3.append(97));//追加 abc97
StringBuffer s3 = new StringBuffer("abcde");
System.out.println(s3.capacity());///能存字符个数
System.out.println(s3.delete(0,2));//删除
System.out.println(s3);
System.out.println(s3.reverse());//反转
System.out.println(s3);
System.out.println(s3.toString() instanceof String);//转成字符串类型并检测是否为字符串类型
// StringBuilder sb = new StringBuilder();//比StringBuffer处理速度快,但不安全
}
}
练习:
//7.求出字符串中有多少种字符,以及每个字符的个数 *
static void printCharInfo(String str){
while(str.length()>0)
{
int h=str.length();
String a = str.charAt(0)+"";
str=str.replace(a,"");
System.out.println(a+":"+(h-str.length()));
}
}
//8.去掉字符串右边的空格
String rtrim(String str){
String s=str.trim();
int index=str.indexOf(s);
String s1=str.substring(0,index+s.length());
return s1;
}