可变字符串和不可变字符串的区别
定义:java将于字符串相关的类容面向的对象了,形成的类就叫字符串类。
字符串的表示方法;
* String string = "字符串"
字符串的分类:
1、不可变字符串
- 定义: String:说的是字符串本身没有发生变化,与引用无关- - -常量。
- 表示方法:String s1 = “1000phone”;
- 实例代码:
public class Demo2 {
public static void main(String[] args) {
String s1 = "1000phone";
String s1 = "1000phone";
String s2 = "1000phone";
String s3 = new String("1000phone");
String s4 = new String("1000phone");
System.out.println(s1 == s2);//true
System.out.println(s1 == s3);//false
System.out.println(s1 == s4);//false
System.out.println(s3 == s4);//false
//说明字符串重写了equals方法,重新指定了比较的规则.规则变成:让s1与s3属性的地址比较
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//true
//总结:以后比较字符串时尽量使用equals
}
- 解释:
- 当执行s1的时候,会到常量区找叫1000phone的字符串,如果有直接让s1保存他的地址,如果没有,会在常量区开辟一块儿空间存储1000phone。
- 执行s2是同理s1。
- 执行s3时,由于进行了new,一定会现在堆中开辟一块儿空间,而1000phone是作为参数传给了对象.保存在了对象的一个String类型的成员变量内,所以直接判断s1与s3不相同。
- 执行s4同理s3。
- 其中的相关方法使用:
public class Demo3 {
public static void main(String[] args) {
String s1 = "1000phone";
//1.判断:
//判断是否包含一个子字符串
//boolean contains(CharSequence s)
System.out.println(s1.contains("1000"));//true
//判断两个字符串的内容是否相同
//boolean equals(Object anObject)
System.out.println(s1.equals("10000phone"));//false
//忽略大小写判断两个字符串的内容是否相同
//boolean equalsIgnoreCase(String anotherString)
System.out.println(s1.equalsIgnoreCase("1000phonE"));//true
//判断是否以某字符串开头
//boolean startsWith(String prefix)
System.out.println(s1.startsWith("1000"));//true
//判断是否以某字符串结尾
//boolean endsWith(String suffix)
System.out.println(s1.endsWith("1000"));//false
//2.转换:将字符数组转换成字符串
char[] arr = {'a','b','c'};
//1:使用构造方法
//String(char[] value)
String s2 = new String(arr);
System.out.println(s2);//abc
//String(char[] value, int offset, int count) 将字符数组的一部分转成字符串
String s3 = new String(arr, 0, 2);
System.out.println(s3);//ab
//2:使用静态方法
//static String copyValueOf(char[] data)
//static String copyValueOf(char[] data, int offset, int count)
//将字符串转成字符数组
//char[] toCharArray()
char[] arr2 = s1.toCharArray();
System.out.println(arr2.length);//9
//将字节数组转成字符串--默认是GBK
//String(byte[] bytes)
byte[] arr3 = {96,97,98};
String s4 = new String(arr3);
System.out.println(s4);
//String(byte[] bytes, int offset, int length)
//String(byte[] bytes, String charsetName)//使用指定的编码将字节数组转换成字符成
//将字符串转成字节数组
//byte[] getBytes()
byte[] arr4 = s4.getBytes();
System.out.println(arr4.length);//3
//将基本数据类型转换成字符串
//String.valueOf()
String s5 = String.valueOf(34);
System.out.println(s5+1);
//3.替换:
//String replace(char oldChar, char newChar)
String s6 = s1.replace("1000", "9999");
System.out.println(s6+" "+s1);//9999phone
//子串:
//String substring(int beginIndex)
String subString1 = s1.substring(2);
System.out.println(subString1);
//String substring(int beginIndex, int endIndex) //包含起始位置,不包含结束位置,到结束位置的前一位
String subString2 = s1.substring(0, 4);
System.out.println(subString2);
//转换,去除空格,比较:
//大小写转换
//String toUpperCase()
String s7 = s1.toUpperCase();
System.out.println(s7);
//String toLowerCase()
String s8 = s7.toLowerCase();
//将字符串两端的空格去掉,中间的无法去除
//String trim()
System.out.println(" ph one ".trim()+"haha");
//按字典顺序比较两个字符串
/*
* 字典顺序:按照ASCII表比较当前的两个字符,ASCII码大的认为是大的字符
* 规则:从左边第一个字符开始比较
* 如果当前的字符不相同,直接认为ASCII大的字符串是大字符串,后面的字符停止比较
* 当前字符比较的具体规则:使用前面的字符-后面的字符,返回差值.如果是负数,说明前面的字符串小于后面的.反之前面的大.
* 如果当前的字符相同,再去比较第二个字符,依次往后推,如果比到最后都相同,则认为两个字符串相等,差值返回0.
*/
//int compareTo(String anotherString)
int value = s1.compareTo("1000hone");
System.out.println(value);
//4.切割: String[] split(String)
String ss = "a,b,c,d";
//String[] strings = ss.split("");//使用""可以进行切割
String[] strings = ss.split(",");//当子字符串作为了刀,就不会再被作为内容.
for (String string : strings) {
System.out.println("子串:"+string);
}
}
}
2、可变字符串
- 定义: 字符串缓冲区
- 表示方法:
- StringBuffer:(jdk1.0),是线程安全的,要考虑线程安全问题。
- StringBuilder:(jdk1.5),是线程不安全的,不需要考虑线程安全问题。
- 两者的区分:在不考虑线程安全的前提下,尽量使StringBuilder,效率高,速度快。
- 实例代码:
public class Demo4 {
public static void main(String[] args) {
//创建空的可变字符串对象
StringBuffer stringBuffer = new StringBuffer();
//1.存储:
//StringBuffer append(boolean b) 从最后插入
stringBuffer.append("1000");
stringBuffer.append("phone");
System.out.println(stringBuffer);
//StringBuffer insert(int offset, boolean b) 从指定位置插入
stringBuffer.insert(0, true);
System.out.println(stringBuffer);
//2.删除:
//StringBuffer delete(int start, int end)
stringBuffer.delete(0, 4);
System.out.println(stringBuffer);
//StringBuffer deleteCharAt(int index)
//3.修改:
//StringBuffer replace(int start, int end, String str) 替换指定的子字符串
//void setCharAt(int index, char ch) 修改一个字符
stringBuffer.replace(0, 4, "9999");
System.out.println(stringBuffer);
//4.获取:
//char charAt(int index)
char c = stringBuffer.charAt(1);
//int indexOf(String str)
int index = stringBuffer.indexOf("pho");
System.out.println(index);//4
//int indexOf(String str, int fromIndex)
int index1 = stringBuffer.indexOf("pho", 6);//-1
System.out.println(index1);
//int lastIndexOf(String str)
//返回指定子字符串(从右边开始计数)在此字符串第一次出现时的索引
//int lastIndexOf(String str, int fromIndex)
//返回指定子字符串(从右边开始计数,范围:当前指定的位置-0)在此字符串第一次出现时的索引
//int length()
//返回长度(字符数)。
System.out.println(stringBuffer.length());
//5.反转:
//StringBuffer reverse()
System.out.println(stringBuffer.reverse());
}
}
理解:可变字符串能够动态的分配字符串的空间。