1.String对象一旦创建完毕,其字符内容不可修改
自变量字符串(String d1=“123”),会在字符串常量池(栈空间一部分)有一部分内存,而new出来的对象,则在栈空间中有内存。
2.intern方法
把字符串加入字符串常量池
public class Main {
public static void main(String[]args){
int a=1,b=2,c=3;
String str1=String.format("%d%d%d",a,b,c);
String str2=String.format("%d%d%d",a,b,c);
String str3=str1.intern();//此时字符串常量池没有123,所以将123存入;
String str4=str2.intern();//此时常量池存在123,str4=str3,或str2;
String str5="123";//1=3,4,5
System.out.println(+'A');//转化为ascll
}
}
3.字符串截取
“字符串”.substring(begin,end)//[begin,end)
“字符串”.indexOf("字符")//查找,如果是不存在返回-1,存在返回其位置
“字符串”.lastIndexOf("字符")//从后往前查找,如果是不存在返回-1,存在返回其位置(数的时候还是从前面计算)
与颜色相对应