字符串
字符串是字符的序列。Java语言在包java.lang中封装了类的String和StringBuffer,分别用于处理不变字符串和可变字符串。两者均被说明为final,意味两者均不含子类。
1. String类
Java中将字符串作为String类型对象来处理。被创建的字符串是不能改变的。当需要改变字符串时,应创建一个新的String对象来保存新的内容,原字符串不变。
一个字符串对象一旦被配置,它的内容就是固定不变。
String str = "Hello";
str = "Good";
在这个程序片段中有两个字符串对象,一个是Hello字符串对象,长度为5;一个是Good字符串对象,长度为4.两个是不同的字符串对象。这段程序不是在Hello字符串后加上Good字符串,而是让str名称引用至Good字符串对象。在Java中,使用“=”将一个字符串对象指定给一个变量名称,其意义为改变该名称所引用的对象。原来被引用的对象若无其他名称来引用它,就会在适当的时候被Java的“垃圾回收”机制回收。
- String对象的创建
1)String(),默认构造方法,无参数。
String s1 = new String();
2)String(char chars[]),传入字符数组。
char[] myChars={'a','b','c'};
String s2 = new String(myChars) //使用字符串“abc”初始化s2
3)String(char chars[],int startIndex,int numChars),传入一个字符数组,从指定下标位置开始获取制定个数的字符,用这些字符来初始化字符串变量。
char[] myChars={'h','e','l','l','o'};
String s3 = new String(myChars,1,3) //使用字符串“ell”初始化s3
4)String(String strObj),传入另一个字符串对象,用该字符串对象的内容初始化。
String s4 = new String(s3); //这时s4也是“ell”
5)String(byte asciiChars[])
String(byte asciiChars[],int startIndex,int numChars)
实例:
public class Example {
public static void main(String[] args) {
byte ascii[] = {65,66,67,68,69,70};
String s1 = new String(ascii);
System.out.println(s1);
String s2 = new String(ascii,2,3);
System.out.println(s2);
}
}
运行结果:
- String的对象连接
1)使用符号“+”把两个字符串连接起来。
String s1 = "Hello";
String s2 = "World";
String S = s1 + s2;
上面的代码将字符串S赋值为“HelloWorld”,注意单词之间没有空格。
2)当连接一个字符串和一个非字符串时,后者将被转化为字符串。
int age = 13;
String rating =“PG” + age;
将把rating的值赋为字符串“PG13”。
这种特性常被用在输出语句中,如:
System.out.println(“The answer is” + answer);
- String对象的编辑
方法 | 说明 |
---|---|
length() | 取得字符串的字符长度 |
equals() | 判断原字符串中的字符是否等于指定字符串中的字符 |
toLowerCase() | 转换字符串的英文字符为小写 |
toUpperCase() | 转换字符串的英文字符为大写 |
public class Example {
public static void main(String[] args) {
String text = "hello";
System.out.println("字符串内容:"+text);
System.out.println("字符串长度:"+text.length());
System.out.println("是否等于hello:" +text.equals("hello"));
System.out.println("转为大写:" +text.toUpperCase());
System.out.println("转为小写:" +text.toLowerCase());
}
}
运行结果:
- String对象的访问
方法 | 说明 |
---|---|
char charAt(int index) | 返回指定索引处的字符 |
int indexOf(int ch) | 返回指定字符第一个找到的索引位置 |
int indexOf(String str) | 返回指定字符串第一个找到的索引位置 |
int lastIndexOf(int ch) | 返回指定字符串最后一个找到的索引位置 |
String substring(int beginIdex) | 取出指定索引处至字符串尾端的子字符串 |
String substring(int beginIndex,int endIndex) | 取出指定索引范围子字符串 |
char[] toCharArray() | 将字符转换为字符数组 |
- 判断String对象是否相等
1)equals方法(实体)
测试两个字符串是否相等。如果字符串p和t相等,p.equals(t)的返回值为true,否则返回值为false。
注意:p和t可以是字符串常量,也可以是字符串变量。
"Hello".equals(t); //合法
"HELLO"equalsIgnoreCase("hello"); //返回值为true,判断两个字符串除大小写区别外是否相等。
2)==
“==”判断两个引用变量是否指向同一引用。
public class Example {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hello");
String s3 = "hello";
String s4 = new String("hello");
System.out.println(s1 == s3); //字符串常量引用同一常量池的对象
System.out.println(s2 == s4); //字符串对象将分别创建
System.out.println(s1.equals(s2));
System.out.println(s3.equals(s4));
}
}
运行结果:
2.StringBuffer类
StringBuffer类表示了可变长度的可写的字符序列。对于StringBuffer类对象,不仅能够进行查找和比较操作,还可以进行添加、插入、修改之类的操作。每个StringBuffer对象都有一个容量,只要其字符序列的长度不超过其容量,就无需分配新的内部缓冲数组,如果内部缓冲数组溢出,StringBuffer对象的容量自动增大。
- 构造方法
public stringBuffer(): 默认构造方法。
public StringBuffer(int length):按照length作为初始容量初始化StringBuffer对象。
public StringBuffer(String Str):按照String对象str为StringBuffer对象初始字符串。
- 主要成员方法
public int length():得到当前StringBuffer的**长度**(字符数)。
public int capacity():得到当前StringBuffer的**容量**。
public synchronized void ensureCapacity(int minCapacity):确保StringBuffer的容量不小于minCapacity。
public synchronized void setLength(int newLength):确保StringBuffer的长度为newLength。
public synchronized char charAt(int index):得到指定位置index的字符。
public synchronized void getChars(int srcBegin,int srcEnd,char dst[],int dstBegin):将StringBuffer中从srcBegin到srcEnd的字符拷贝到数组dst[] (开始位置为dstBegin)。
3.String类与StringBuffer类的比较
- 相同点
① 都用来处理字符串;
②都提供了length()、toString()、charAt()和subString()方法,它们的用法在两个类中相同;
③字符在字符串中的索引位置都从0开始。
- 不同点
①String类是不可变类,而StringBuffer类是可变类。String类的subString()、concat()、toLowerCase()、toUpperCase()和trim()等方法都不会改变字符串本身,而是创建并返回一个包含改变后内容的新字符串对象。StringBuffer的append()、replaceAll()、replaceFirst()、insert()和setCharAt()等方法都会改变字符缓冲区中的字符串内容。
②String类覆盖了Object类的equals()方法,而StringBuffer类没有覆盖。
③ 两个类都覆盖了Object类的toString()方法,但各自实现方是不同,String类中的toString()方法返回了当前String实例本身的引用,StringBuffer类的toString()方法返回一个以当前StringBuffer的缓冲区中的所有字符为内容的心String对象的引用。
④ String类对象可用操作符“+”进行连接,而StringBuffer类对象之间不行。