String类、StringBuffer类和StringBuilder类 String类和StringBuffer类都在Java的lang包中,并被定义为最终(final) 类,不能再派生子类
String类被Java的开发者构造得非常接近基本数据类型,换句话说,在很多时候可以象用基本数据类型一样来使用tring类
String s; //声明了一一个空字符串sString ss= "创建了-一个字符串"; String s = new String(); String ss = new String("创建了一一个字符串");String sa = new String]{"创建了一一个字符串","数组"} ;
用括在双引号""里面的一串字符串做参数构造字符串
public
String(String original);
用字符数组做参数构造字符串
public String(char value[]);public String(char value[],int offset, intcount);
用指定的缓冲字符串对象buffer构造字符串对象
public
String(StringBuffer buffer);
用字节数组作为参数构造字符串
public String(byteD bytes);public String(byteD bytes,String enc);public String(byte[ bytes,int offset, int length);public String(byte[ bytes,int offset, int length, String enc);
●获取一个字符串的长度和串中指定位置的字符 ●判断一个字符串的前缀和后缀 ●判断一一个字符串是否包含指定的字符子串或指定的字符 ●两个字符串对象恒等比较 ●两个字符串对象大小比较 ●字符串连结,字符替换和字符串大小写转换 ●字符串截取 ●把基本数据类型转换为字符串 ●把字符串转换为字节数组或字符数组
●Java中可以使用关系运算符“==”判定两个字符串是否相等 ●与equals(方法不同的是,“==”判定两字符串对象是否是同一实例,即它们在内存中的存储空间是否相同
1、Java中可以使用关系运算符“==”判定两个字符串是否相等。 2、与equals()方法不同的是,“==”判定两字符串对象是否是同一实例,即它们在内存中的存储空间是否相同。
class StringTest2 {
public static void main(String args[]) {
String s1 = "This is the second string.";
String s2 = "This is the second string.";
String s3 = new String("This is the second string.");
String s4 = new String(s1);
String s5 = s1;
boolean result121 = s1.equals(s2);
boolean result122 = s1 == s2;
boolean result131 = s1.equals(s3);
boolean result132 = s1 == s3;
boolean result141 = s1.equals(s4);
boolean result142 = s1 == s4;
boolean result151 = s1.equals(s5);
boolean result152 = s1 == s5;
System.out.println("s1 equals s2= " + result121);
System.out.println("s1 == s2= " + result122);
System.out.println("s1 equals s3= " + result131);
System.out.println("s1 == s3= " + result132);
System.out.println("s1 equals s4= " + result141);
System.out.println("s1 == s4= " + result142);
System.out.println("s1 equals s5= " + result151);
System.out.println("s1 == s5= " + result152);
}
}
public class StringToArray {
public static void main(String[] args) {
String s = "ABCDE";
byte b[] = s.getBytes();
System.out.println("\t" + s);
for (int i = 0; i < b.length; i++)
System.out.print("\t" + b[i]);
System.out.println();
char ch0[] = s.toCharArray();
myPrint(ch0);
char ch1[] = new char[3];
s.getChars(1, 4, ch1, 0);
myPrint(ch1);
}
static void myPrint(char[] obj) {
for (int i = 0; i < obj.length; i++)
System.out.print("\t" + obj[i]);
System.out.println();
}
}
系统为String类对象分配内存时,按照对象中所含字符的实际个数等量分配 而为StringBuffer类对象分配内存时,除去字符所占空间外,再另加16个字符大小的缓冲区
常用方法 ●获取和设置字符串缓冲区内字符串的长度、获取字符串缓冲区的容量大小 ●在字符串缓冲区的字符串末尾添加各种类型的数据 ●在缓冲字符串的指定位置插入各种类型的数据 ●删除和替换缓冲字符串中的字符或字符串 ●获取或更该指定的字符
public StringBuffer append--);//共有11个append方法public StringBuffer insert(..);//共有10个insert方法public StringBuffer delete(int start, int end);public StringBuffer deleteCharAt(int index);public int length(); //值为字符串长度public int capacity()); //值为字符串长度+16
public StringBuffer reverse(int start, int end, String str);public StringBu£fer replace(int start, int end, String s);public String toString(;public String substring(int start);public String substring(int start, int end);
获取和设置字符串缓冲区内字符串的长度、获取字符串缓冲区的容量大小
public int length(;
public int capacity0; //其值为字符串长度+16
例如:
StringBuffer buf = new StringBuffer("0123");
buflength()= 4;
buf.capacity() = 4+16 = 20;
一般地,StringBuffer的长度(length)是指存储在其中的字符个数,容量(capacity)是指缓冲区所能容纳的最大字符数
public class StringBufferDemo { public static void main(String[] args) { String s = new String("This is A String"); StringBuffer sb = new StringBuffer(s); sb.replace(8, 16, "a StringBuffer"); sb.append("Demo."); System.out.println(sb.toString()); int len = sb.length(); sb.reverse().append('\n').append("the length of sb = ").append(len) .append(',').append(true).append('!'); System.out.println(sb.toString()); }}
●StringBuilder和StringBuffer非常类似,均代表可变的字符序列,而且方法也一样 ●String: 不可变字符序列 ●StringBuilder: 可变字符序列、效率高、线程不安全 ●StringBuffer: 可变字符序列、效率低、线程安全
strings="a"; //创建了一个字符串s=s+"b"; //实际上原来的"a"字符串对象已经丢弃了,现在又产生了一个字符串s+"b" (也就是"ab")。如果多次执行这些改变串内容的操作,会导致大量副本字符串对象存留在内存中,降低效率。如果这样的操作放到循环中,会极大影响程序的性能。
●如果创建一个字符串,不打算修改它,用String ●如果创建一一个字符串,计划修改它,用StringBuffer ●String比StringBuffer执行效率高 ●StringBuffer比String方便 ●StringBuilder:可变字符序列、效率高、线程不安全 StringBuffer:可变字符序列、效率低、线程安全
推荐阅读