java将字符串直接面向对象了,形成的类就是字符串类,表示方法:" "
分类:
注意:可变字符串与不可变字符串的直接父类都是Object,他们之间是兄弟,没有任何关系.
不可变字符串本身可以认为是常量
字符串常量的保存位置:常量区.可以任务字符串的常量区是堆区中一块儿特殊的区域.
字符串常量的特点:同一个名字的字符串常量在内存中只允许存在一个.(只占用一块儿内存)
public class Demo3 {
public static void main(String[] args) {
// //创建的不可变字符串
// String s = "hello";
// //创建的可变字符串
// StringBuffer stringBuffer = new StringBuffer("world");
//先讲不可变字符串
/*
* 当执行s1的时候,会到常量区找叫1000phone的字符串,如果有直接让s1保存他的地址,如果没有,会在常量区开辟一块儿
* 空间存1000phone.
* 执行s2是同理s1
* 执行s3时,由于进行了new,一定会现在堆中开辟一块儿空间,而1000phone是作为参数传给了对象.保存在了对象的一个String
* 类型的成员变量内,所以直接判断s1与s3不相同.
* 执行s4同理s3
* */
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
//说明字符串重写了equals方法,重新定义了比较的规则,规则变成:如果是直接的两个字符串比较,就比较地址
//如果是比较的字符串对象,就去比较对象内部的属性的地址.
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//true
//综上:以后大家进行字符串比较的时候,尽量使用equals
}
}
转换:将字符数组转换成字符串
替换:
切割: String[] split(String)
public class Demo4 {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "1000phone";
System.out.println(s.contains("1000"));
System.out.println(s.equals("1000Phone"));//false
System.out.println(s.equalsIgnoreCase("1000Phone"));//true
System.out.println(s.startsWith("1000"));//true
System.out.println(s.endsWith("phone"));//true
//2.转换:将字符数组转换成字符串
char[] arr = {'a','d','y','g'};
//1:使用构造方法
//String(char[] value)
String s2 = new String(arr);
System.out.println(s2);
//String(char[] value, int offset, int count) 将指定个数的字符转成字符串,offset:是起始点 count:是字符个数
String s3 = new String(arr, 0, 2);
System.out.println(s3);
//2:使用静态方法
//static String copyValueOf(char[] data)
//static String copyValueOf(char[] data, int offset, int count)
//将字符串转成字符数组
//char[] toCharArray()
char[] arr1 = s.toCharArray();
for (char c : arr1) {
System.out.println("value:"+c);
}
//将字节数组转成字符串
byte[] arr2 = {97,98,99,100};
//String(byte[] bytes)
String s4 = new String(arr2);
System.out.println(s4);
//String(byte[] bytes, int offset, int length)
s4 = new String(arr2, 0, 2);
System.out.println(s4);
//String(byte[] bytes, String charsetName)//使用指定的编码将字节数组转换成字符串
byte[] arr3 = {-60,-29,-70,-61};
s4 = new String(arr3,"GBK");
System.out.println(s4);
//将字符串转成字节数组
//byte[] getBytes()
byte[] arr4 = s4.getBytes();//这里使用的是默认字符集,现在是utf8,所以一个汉字对应3个字节
for (byte b : arr4) {
System.out.print("value:"+b);//-28,-67,-96,-27,-91,-67
}
System.out.println();
//将基本数据类型转换成字符串
//String.valueOf()
String s5 = String.valueOf(12);
System.out.println(s5+1);
//3.替换:
//String replace(char oldChar, char newChar)
String s6 = s.replace("1000", "****");
System.out.println(s6+" "+s);//s的值还是1000phone,说明字符串是常量,本身不会发生改变
//子串:
//String substring(int beginIndex) 取值时包括当前的位置
System.out.println(s.substring(4));
//String substring(int beginIndex, int endIndex) //包含起始位置,不包含结束位置,到结束位置的前一位
System.out.println(s.substring(4,6));
//转换,去除空格,比较:
//大小写转换
//String toUpperCase()
String s7 = s.toUpperCase();
System.out.println(s7);
//String toLowerCase()
s7 = s7.toLowerCase();
System.out.println(s7);
//将字符串两端的空格去掉,注意:中间的空格不会去掉
//String trim()
String s8 = " he llo ";
s8 = s8.trim();
System.out.println(s8+"ok");
//按字典顺序比较两个字符串
//int compareTo(String anotherString)
System.out.println(s.compareTo("9000ahone"));
//4.切割: String[] split(String)
String s9 = "s,t,u,d,,e,nt";
String[] strings = s9.split("");
for (String string : strings) {
System.out.println(string);
}
}
}
存储:
删除:
修改:
获取:
反转:
public class Demo5 {
public static void main(String[] args) {
//创建空的可变字符串
StringBuffer stringBuffer1 = new StringBuffer();
//1.存储:
//StringBuffer append(boolean b) 从最后插入
stringBuffer1.append("1000");
stringBuffer1.append("phone");
System.out.println(stringBuffer1);
//StringBuffer insert(int offset, boolean b) 从指定位置插入
System.out.println(stringBuffer1.insert(4, "bingbing"));
//2.删除:
//StringBuffer delete(int start, int end)
System.out.println(stringBuffer1.delete(4, 12));
//StringBuffer deleteCharAt(int index)
//System.out.println(stringBuffer1.deleteCharAt(0));
//3.修改:
//StringBuffer replace(int start, int end, String str) 替换指定的子字符串
System.out.println(stringBuffer1.replace(0, 4, "bingbing"));
//void setCharAt(int index, char ch) 修改一个字符
//4.获取:
//char charAt(int index)
System.out.println(stringBuffer1.charAt(0));
//int indexOf(String str)从左到右查
System.out.println(stringBuffer1.indexOf("phone"));
//int indexOf(String str, int fromIndex) 从指定的位置按照从左到右的顺序开始查
System.out.println(stringBuffer1.indexOf("bing",2));
//int lastIndexOf(String str)
//返回指定子字符串(从右边开始计数)在此字符串第一次出现时的索引
//int lastIndexOf(String str, int fromIndex)
//返回指定子字符串(从右边开始计数,范围:当前指定的位置-0)在此字符串第一次出现时的索引
//int length()
//返回长度(字符数)。
System.out.println(stringBuffer1.length());
//5.反转:
//StringBuffer reverse()
System.out.println(stringBuffer1.reverse());
}
}
将字符串中指定部分进行反转,“abcdefg”;abfedcg
思路:
public class Demo6 {
public static void main(String[] args) {
String string = "aaaahellobbbbb";
String string2 = exchange(string, 4, 9);
System.out.println(string2);
}
public static String exchange(String source,int begin,int end){//这里end作为下标的值不包含在反转范围内
//先将字符串转成字符数组
char[] arr = source.toCharArray();
for (int i = begin,j = end-1; i < j; i++,j--) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return new String(arr);
}
}