1.String类的理解
1.String对象用于保存字符串,也就是一组字符序列。 2.字符串常量使用双引号括起的字符序列。 3 字符串的字符使用Unicode字符编码,一个字符占两个字节(不区分字母和汉字) 4.String类常用的构造方法 String s1=new String(); String s2=new String(String original); String s1=new String(char []a); String s1=new String(char []a,int startIdex,int count); //5.String类实现了接口Serializable(String可以串行化:可以在网络传输) //和接口Compara(String对象可以比较大小). //6.String是final类,不能被其它类继承 //7.String有属性private final char value[];用于存放字符串内容. 8.value是一个final类型,不可以修改,即value不能指向新的地址,但是单个字符是可以变化的
package com.demo.string;
public class String01 {
public static void main(String[] args) {
String name="Demo龙";
name="小黑";
final char value[]={'d','e','m','o'};
value[3]='l';
char ch[]={'a','b','c'};
char value02[]={'d','e','m','o'};
value[3]='l';
char ch02[]={'a','b','c'};
value02=ch02;
}
}
2.创建String对象的两种方式
1.常量式创建
在常量池中拿对象
String b="demo龙";
String c="kkkkk";
2.对象式创建
直接在堆内存空间创建一个新的对象。
String s1 = new String("demo龙");
String s1 = new String("tttttt");
3.String字符串的拼接
String s1 = new String("demo龙");
String s2 = new String("tttttt");
String str1 = "str";
String str2 = "ing";
String str3 = "str" + "ing";
String str4 = s1 + s2;
String str5 = "string";
System.out.println(str3 == str4);
System.out.println(str3 == str5);
System.out.println(str4 == str5);
运行结果
3.String和StringBuffer的相互转换
1.String转化StringBuffer
1.方法一:构造法
StringBuffer sb03=new StringBuffer(s1);
StringBuffer sb04=new StringBuffer(s2);
StringBuffer sb06=new StringBuffer("demo猫");
2.方法2:append
StringBuffer sb05=new StringBuffer();
sb05.append(b);
StringBuffer sb07=new StringBuffer();
sb07.append("hhhh");
package com.demo.Wrapper;
public class test01 {
public static void main(String[] args) {
Integer i = Integer.valueOf(5);
String a;
String b="demo虎";
String c="kkkkk";
String s1 = new String("demo龙");
String s2 = new String("tttttt");
StringBuffer sb1=new StringBuffer("demo熊");
StringBuffer sb2=new StringBuffer("凯尔特人");
StringBuffer sb03=new StringBuffer(s1);
StringBuffer sb04=new StringBuffer(s2);
StringBuffer sb06=new StringBuffer("demo猫");
StringBuffer sb05=new StringBuffer();
sb05.append(b);
StringBuffer sb07=new StringBuffer();
sb07.append("hhhh");
System.out.println("sb03="+sb03);
System.out.println("sb04="+sb04);
System.out.println("sb05="+sb05);
System.out.println("sb06="+sb06);
System.out.println("sb07="+sb07);
}
}
运行结果
Stringbuffer转化String
1.方法一:构造法
String s3=new String(sb1);
String s4=new String(sb2);
2.方法二:toString
String s5=sb06.toString();
String s6=sb07.toString();
package com.demo.Wrapper;
public class test01 {
public static void main(String[] args) {
Integer i = Integer.valueOf(5);
String a;
String b="demo虎";
String c="kkkkk";
String s1 = new String("demo龙");
String s2 = new String("tttttt");
StringBuffer sb1=new StringBuffer("demo熊");
StringBuffer sb2=new StringBuffer("凯尔特人");
StringBuffer sb03=new StringBuffer(s1);
StringBuffer sb04=new StringBuffer(s2);
StringBuffer sb06=new StringBuffer("demo猫");
StringBuffer sb05=new StringBuffer();
sb05.append(b);
StringBuffer sb07=new StringBuffer();
sb07.append("hhhh");
String s3=new String(sb1);
String s4=new String(sb2);
String s5=sb06.toString();
String s6=sb07.toString();
System.out.println("s3="+s3);
System.out.println("s4="+s4);
System.out.println("s5="+s5);
System.out.println("s6="+s6);
}
}
测试结果
4.StringBuffer方法介绍
//1.StringBuffer是线程安全(耗费资源)的可变序列 //2.StringBuilder是线程不安全(不耗费资源)的可变序列
1.打印容量(理论值)capacity()
2.打印长度(实际值) length()
3.添加append()
4.打印内容toString()
5.设置长度setLength()
6.插入insert()
7.替换索引位置的字符setCharAt()
8.删除delete()
9.替换replace()
10.反转reverse()
package com.demo.Wrapper;
public class test01 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity());
System.out.println(sb.length());
StringBuffer sb1 = new StringBuffer();
sb1.append("wukong").append("z").append("n");
sb1.append("z");
sb1.append("n123456789");
System.out.println(sb1.capacity());
System.out.println(sb1);
System.out.println(sb1.toString());
StringBuffer sb3 = new StringBuffer();
sb3.setLength(10);
System.out.println(sb3.length());
System.out.println(sb3.capacity());
sb3.insert(0, "wukong");
System.out.println(sb3.toString());
sb3.setCharAt(5,'p');
System.out.println(sb3.toString());
sb3.insert(6, 5);
System.out.println(sb3.toString());
StringBuffer sb4 = new StringBuffer();
sb4.append("0123456789");
sb4.delete(1,5);
System.out.println(sb4);
sb4.deleteCharAt(2);
System.out.println(sb4);
StringBuffer sb5 = new StringBuffer();
sb5.append("wukong");
sb5.replace(0, 4,"demo");
System.out.println(sb5);
String string = "wukong";
StringBuffer s6 = new StringBuffer(string);
s6.reverse();
string = s6.toString();
System.out.println(string);
}
}
测试结果
|