常用实用类
string类
Java把String类声明为final类,因此用户不能扩展String类,即String类不可以有子类
1.String 对象
(1)可以使用String类声明对象并创建对象
String s = new String("we are students");
String t = new String("we are students");
new 开辟了String对象的空间,对象变量s和t中存放着引用,表明自己的实体的位置,即new 运算符首先分配内存空间并在内存空间中放入字符序列
尽管s和t的实体相同,都是字符序列:we are students,但二者的引用是不同的(如图8.2所),即表达式s==t的值是false(new运算符如它名字一样。r每次都要开辟新天地)。
(2)用字符数组创建String对象
char a[] = {'J','a','v','a'};
String s = new String(a);//a为字符数组的引用
//约等于
String s = new String("Java");
(3)String(char a[],int startIndex,int count) 提取字符数组a中的一部分字符创建一个String对象,参数startIndex和count分别指定在a中提取字符的起始位置和从该位置开始截取的字符个数,如:
char a[] = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
String s = new String(a,2,4);
相当于
String s = new String("贰叁肆伍");
用户无法输出String对象的引用
Syste.out.println(s);
(4)引用String常量
String常量是对象,因此可以把String常量的引用赋值给一个String对象,例如
String s1,s2;
s1 = "你好";
s2 = "你好";
s1,s2具有相同的引用(12AB),表达式s1==s2的值是true,
因而具有相同的实体。s1,s2内存示意如图8.3所示。
由于用户程序无法知道常量池中“你好”的引用,那么把String常量的引用赋值给一个String对象s1时,Java让用户直接写常量的实体内容来完成这一任务,但实际上赋值到String对象s1中的是String常量“你好”的引用(见图8.3)。s1是用户声明的String对象,s1中的值是可以被改变的,如果再进行s1 = “boy”,那么s1中的值将发生变化(变成78CD567a) 。
2.字符串的并置
String对象可以用“+”进行并置运算,即首尾相接得到一个新的String对象。
String you = "你";
String hi = "好";
String testOne;
testOne = you+hi;
如果是两个常量进行并置运算,那么得到的仍然是常量,如果常量池没有这个常量就放入常量池。“你”+“好"的结果就是常量池中的"你好”
public class Example8_1 {
public static void main(String args[]) {
String hello = "你好";
String testOne = "你"+"好"; //【代码1】
int address =System.identityHashCode("你好");
System.out.printf("\"你好\"的引用:%x\n",address);
address =System.identityHashCode(hello);
System.out.printf("hello的引用:%x\n",address);
address =System.identityHashCode(testOne);
System.out.printf("testOne的引用:%x\n",address);
System.out.println(hello == testOne); //输出结果是true
System.out.println("你好" == testOne); //输出结果是true
System.out.println("你好" == hello); //输出结果是true
String you = "你";
String hi = "好";
String testTwo = you+hi; //【代码2】
address =System.identityHashCode("你");
System.out.printf("\"你\"的引用:%x\n",address);
address =System.identityHashCode("好");
System.out.printf("\"好\"的引用:%x\n",address);
address =System.identityHashCode(testTwo);
System.out.printf("testTwo的引用:%x\n",address);
System.out.println(hello == testTwo); //输出结果是false
}
}
运行结果:
testTwo = you+hi; 的赋值号的右边有变量,例如变量you参与了并置运算,那么you+hi相当于new String("你好");
因此结果在动态区诞生新对象,testTwo存放着引用568db2f2,testTwo的实体中存放者字符序列:你好(如图8.4),
所以表达式hello == testTwo的结果是false
String类的常用方法
1.public int length()
获取一个字符串的长度 .
2.public boolean equals(String s)
判断当前String对象的字符序列是否与参数s指定的String对象的字符序列相同
public class Example8_2 {
public static void main(String args[]) {
String s1,s2;
s1 = new String("天道酬勤");
s2 = new String("天道酬勤");
System.out.println(s1);
System.out.println(s2);
System.out.println("二者的实体相同吗:"+s1.equals(s2)); //true
int addressS1 =System.identityHashCode(s1);
int addressS2 =System.identityHashCode(s2);
System.out.printf("String对象s1和s2的引用分别是%x,%x\n",addressS1,addressS2);
System.out.printf("二者的引用相同吗:%b\n",s1==s2); //false
String s3,s4;
s3 = "we are students";
s4 = new String("we are students");
System.out.println(s3);
System.out.println(s4);
System.out.println("二者的实体相同吗:"+s3.equals(s4));//true
int addressS3 =System.identityHashCode(s3);
int addressS4 =System.identityHashCode(s4);
System.out.printf("String对象s3和s4的引用分别是%x,%x\n",addressS3,addressS4);
System.out.printf("二者的引用相同吗:%b\n",s3==s4); //false
String s5,s6;
s5 = "勇者无敌";
s6 = "勇者无敌";
System.out.println(s5);
System.out.println(s6);
System.out.println("二者的实体相同吗:"+s5.equals(s6)); //true
int addressS5 =System.identityHashCode(s5);
int addressS6 =System.identityHashCode(s6);
System.out.printf("String对象s5和s6的引用分别是%x,%x\n",addressS5,addressS6);
System.out.printf("二者的引用相同吗:%b\n",s5==s6);//true
}
}
3.public boolean startsWith(String s)
判断当前String对象的字符序列前缀是否是参数指定的String对象s的字符序列,例如:
String tom = "天气预报,阴有小雨";
tom.startsWith("天气")的值是true;
tom.endsWith("大雨")的值是false,
4. public int compareTo(String s):
按字典序与参数s指定的字符序列比较大小。
如果当前String对象的字符序列与s的相同,该方法返回值0,如果大于s的字符序列,
//使用compareTo对字符序列排序,插入排序
public class SortString {
public static void sort(String a[]) {
int count=0;
for(int i=0;i < a.length-1;i++) {
int saveMinIndex = i;
for(int j=i+1;j<a.length;j++) {
if(a[j].compareTo(a[saveMinIndex])<0) {
saveMinIndex = j;
}
}
String temp=a[i];
a[i]=a[saveMinIndex];
a[saveMinIndex]=temp;
}
}
}
5.public boolean contains(String s)
String对象调用contains方法判断当前String对象的字符序列是否包含参数s的字符序列,例如,tom=“student”,那么tom.contains(“stu”)的值就是true,而tom.contains(“ok”)的值是false。
。
6.public int indexOf (String str)
String对象调用方法从当前String对象的字符序列的0索引位置开始检索首次出现str的字符序列的位置,并返回该位置。如果没有检索到,该方法返回的值是–1,其相关方法:
indexOf(String s ,int startpoint)
lastIndexOf (String s)
例如 String tom = "I am a good cat";
tom.indexOf("a");//值是2
tom.indexOf("good",2);//值是7
tom.indexOf("a",7);//值是13
tom.indexOf("w",2);//值是-1
7. public String substring(int startpoint)
字符串对象调用该方法获得一个新的String对象,新的String对象的字符序列是复制当前String对象的字符序列中的startpoint位置至最后位置上的字符所得到的字符序列。
String对象调用substring(int start ,int end)方法获得一个新的String对象,新的String对象的字符序列是复制当前String对象的字符序列中的start位置至end–1位置上的字符所得到的字符序列。
8.public String trim()
得到一个新的String对象,这个新的String对象的字符序列是当前String对象的字符序列去掉前后空格后的字符序列。
正则表达式
例题1:
import java.util.Scanner;
public class Example8_9 {
public static void main (String args[ ]) {
String regex = "[a-zA-Z|0-9|_]+";
String regexDigit = "-?[1-9]\\d*";
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
if(str.matches(regex)) {
System.out.println(str+"是英文字母,数字或下划线构成");
if(str.matches(regexDigit))
System.out.println(str+"数字构成");
}
else {
System.out.println(str+"中有非法字符");
}
}
}
例题2:
public class Example8_10 {
public static void main (String args[ ]) {
String str = "培训学校的email:qinghua@sina.com.cn或zhang@163.com";
String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
System.out.println("清除\n"+str+"\n中的email地址");
str = str.replaceAll(regex,"");
System.out.println(str);
String money = "89,235,678¥";
System.out.print(money+"转化成数字:");
String s = money.replaceAll("[,\\p{Sc}]","") ; //\\p{Sc}匹配任何货币符号
long number = Long.parseLong(s);
System.out.println(number);
}
}
import java.util.Scanner;
public class Example8_11 {
public static void main (String args[ ]) {
System.out.println("一行文本:");
Scanner reader=new Scanner(System.in);
String str = reader.nextLine();
//regex匹配由空格、数字和!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~组成的字符序列
String regex = "[\\s\\d\\p{Punct}]+";
String words[] = str.split(regex);
for(int i=0;i<words.length;i++){
int m = i+1;
System.out.println("单词"+m+":"+words[i]);
}
}
}
StringBuffer类
String对象的字符序列是不可修改的,也就是说,String对象的字符序列的字符不能被修改、删除,即String对象的实体是不可以再发生变化的。
StringBuffer类的对象的实体的内存空间可以自动地改变大小,便于存放一个可变的字符序列。比如,对于:
StringBuffer s = new StringBuffer(“我喜欢”);
对象s可调用append方法追加一个字符序列,如图8.17。
s.append(“玩篮球”);