《常用实用类》PPT课件.ppt
《《常用实用类》PPT课件.ppt》由会员分享,可在线阅读,更多相关《《常用实用类》PPT课件.ppt(82页珍藏版)》请在三一办公上搜索。
1、第9章 常用实用类,Java专门提供了用来处理字符序列的String类String类在java.lang包中,由于java.lang包中的类被默认引入,因此程序可以直接使用String类。需要注意的是Java把String类声明为final类,因此用户不能扩展String类,即String类不可以有子类,9.1 String类,可以使用String类来创建一个字符串变量,字符串变量是对象。1常量对象字符串常量对象是用双引号括起的字符序列如:“123”,“abc”等等2字符串对象可以使用String类声明字符串对象String s;s=new String(“你好”);,9.1.1 构造字符串对
2、象,(1)String(char a):用一个字符数组a创建一个字符串对象 char a=J,a,v,a;String s=new String(a);等价于String s=new String(Java);(2)String(char a,int startIndex,int count)提取字符数组a中的一部分字符创建一个字符串对象,参数startIndex和count分别指定在a中提取字符的起始位置和从该位置开始截取的字符个数,String类还有两个较常用构造方法,String类还有两个较常用构造方法,(2)String(char a,int startIndex,int count)
3、提取字符数组a中的一部分字符创建一个字符串对象,参数startIndex和count分别指定在a中提取字符的起始位置和从该位置开始截取的字符个数 char a=J,a,v,a;String s=new String(a,1,3);,字符串常量是对象,因此可以把字符串常量的引用赋值给一个字符串变量 String s1,s2;s1=how are you;s2=how are you;,3引用字符串常量对象,1public int length()使用String 类中的length()方法可以获取一个字符串的长度 String s1;s1=“abc;n=s1.length();2public b
4、oolean equals(String s)字符串对象调用equals(String s)方法比较当前字符串对象的实体是否与参数s指定的字符串的实体相同,9.1.2 String 类的常用方法,equals举例,String s1,s2,s3;s1=how are you;s2=how are you;s3=Hi;s1.equals(s2)的值是trues1.equals(s3)的值是false,9-1,public class Example9_1 public static void main(String args)String s1,s2;s1=new String(天道酬勤);s2
5、=new String(天道酬勤);System.out.println(s1.equals(s2);/输出结果是:true System.out.println(s1=s2);/输出结果是:false String s3,s4;s3=勇者无敌;s4=勇者无敌;System.out.println(s3.equals(s4);/输出结果是:true System.out.println(s3=s4);/输出结果是:true,3public boolean startsWith(String s)、public boolean endsWith(String s)方法字符串对象调用startsW
6、ith(String s)方法,判断当前字符串对象的前缀是否是参数s指定的字符串 String tom=天气预报;tom.startsWith(“天气”)的值是true,4public int compareTo(String s)方法字符串对象可以使用String类中的compareTo(String s)方法,按字典序与参数s指定的字符串比较大小。如果当前字符串与s相同,该方法返回值0;如果当前字符串对象大于s,该方法返回正值;如果小于s,该方法返回负值,9-2,import;public class SortString public static void sort(String a)
7、int count=0;for(int i=0;ia.length-1;i+)for(int j=i+1;ja.length;j+)if(pareTo(ai)0)count+;(交换%s和%s:,ai,aj);String temp=ai;ai=aj;aj=temp;(第+count+次排序结果:);System.out.println(Arrays.toString(a);,5public boolean contains(String s)字符串对象调用contains方法,判断当前字符串对象是否含有参数指定的字符串s String tom=student tom.contains(“st
8、u”)的值为truetom.contains(“ok”)的值为false,6.public int indexOf(String s)从当前字符串的头开始检索字符串s,并返回首次出现s的索引位置。如果没有检索到字符串s,该方法返回的值是-1。字符串调用indexOf(String s,int startpoint)方法从当前字符串的startpoint位置处开始检索字符串s,并返回首次出现s的索引位置。如果没有检索到字符串s,该方法返回的值是-1。字符串调用lastIndexOf(String s)方法从当前字符串的头开始检索字符串s,并返回最后出现s的索引位置。如果没有检索到字符串s,该方法
9、返回的值是-1。,举例,String tom=I am a good cattom.indexOf(a)值为2tom.indexOf(good)的值是7tom.indexOf(a,7)值为13tom.indexOf(w,2)值为-1,7.public String substring(int startpoint)字符串对象调用该方法获得一个当前字符串的子串,该子串是从当前字符串的startpoint处截取到最后所得到的字符串。String s=abc;s.substring(1)的值是bc字符串对象调用substring(int start,int end)方法获得一个当前字符串的子串,该子
10、串是从当前字符串的star索引位置截取到end索引位置所得到的字符串,但不包括end索引位置上的字符,8public String trim()一个字符串s通过调用方法trim()得到一个字符串对象,该字符串对象是s去掉前后空格后的字符串。,9-3,public class Example9_3 public static void main(String args)String path=c:bookjavabookJava Programmer.doc;int index=path.indexOf();index=path.indexOf(,index);String sub=path.s
11、ubstring(index);System.out.println(sub);/输出结果是:bookjavabookJava Programmer.doc index=path.lastIndexOf();System.out.println(index);sub=path.substring(index+1);System.out.println(sub);/输出结果是:Java Programmer.doc System.out.println(sub.contains(Programmer);/输出结果是:true,java.lang包中的Integer类调用其类方法:public s
12、tatic int parseInt(String s)可以将由“数字”字符组成的字符串,转化为int型数据 int x;String s=123;x=Integer.parseInt(s);,9.1.3 字符串与基本数据的相互转化,类似地,使用java.lang包中的Byte、Short、Long、Float、Double类调相应的类方法:public static byte parseByte(String s)throws NumberFormatExceptionpublic static short parseShort(String s)throws NumberFormatExc
13、eptionpublic static long parseLong(String s)throws NumberFormatExceptionpublic static float parseFloat(String s)throws NumberFormatExceptionpublic static double parseDouble(String s)throws NumberFormatException可以将由“数字”字符组成的字符串,转化为相应的基本数据类型,可以使用String 类的下列类方法:public static String valueOf(byte n)publi
14、c static String valueOf(int n)public static String valueOf(long n)public static String valueOf(float n)public static String valueOf(double n)将形如123、1232.98等数值转化为字符串,将数字转化为字符串的方法,9-4,public class Example9_4 public static void main(String args)double aver=0,sum=0,item=0;boolean computable=true;for(Str
15、ing s:args)try item=Double.parseDouble(s);sum=sum+item;catch(NumberFormatException e)(您键入了非数字字符:+e);computable=false;if(computable)System.out.println(sum=+sum);,说明,main方法中的参数args能接收用户从键盘键入的字符串。要求在解释字节码的时候输入args数组中的元素:如Java Example9_4 12 33 19这时,args0=“12”,args1=“33”,args2=“19”,所有的类都默认是java.lang包中Obj
16、ect类的子类或间接子类。Object类有一个public String toString()方法,一个对象通过调用该方法可以获得该对象的字符串表示。一个对象调用toString()方法返回的字符串的一般形式为:创建对象的类的名字对象的引用的字符串表示,9.1.4 对象的字符串表示,9-5,public class TV String name;public TV()public TV(String s)name=s;public String toString()String oldStr=super.toString();return oldStr+n这是电视机,品牌是:+name;,9-
17、5,import;public class Example9_5 public static void main(String args)Date date=new Date();System.out.println(date.toString();TV tv=new TV(长虹电视);System.out.println(tv.toString();,1字符串与字符数组String类也提供了将字符串存放到数组中的方法:public void getChars(int start,int end,char c,int offset)字符串调用getChars()方法将当前字符串中的一部分字符复
18、制到参数c指定的数组中,将字符串中从位置start到end-1位置上的字符复制的数组c中,并从数组c的offset处开始存放这些字符,9.1.5 字符串与字符、字节数组,还有一个简练的将字符串中的全部字符存放在一个字符数组中的方法:public char toCharArray()字符串对象调用该方法返回一个字符数组,该数组的长度与字符串的长度相等、第i单元中的字符刚好为当前字符串中的第i个字符。,toCharArray方法,9-6,public class Example9_6 public static void main(String args)char a,b,c;String s=2
19、009年10月1日是国庆60周年;a=new char2;s.getChars(11,13,a,0);System.out.println(a);c=十一长假期间,学校都放假了.toCharArray();for(char ch:c)System.out.print(ch);,String类的构造方法String(byte)用指定的字节数组构造一个字符串对象。String(byte,int offset,int length)构造方法用指定的字节数组的一部分,即从数组起始位置offset开始取length个字节构造一个字符串对象。,2字符串与字节数组,public byte getBytes(
20、)方法使用平台默认的字符编码,将当前字符串转化为一个字节数组。public byte getBytes(String charsetName)使用参数指定字符编码,将当前字符串转化为一个字节数组。如果平台默认的字符编码是:GB_2312(国标,简体中文),那么调用getBytes()方法等同于调用getBytes(GB2312),public class Example9_7 public static void main(String args)byte d=Java你好.getBytes();(数组d的长度是:+d.length);String s=new String(d,6,2);/输
21、出:好 System.out.println(s);s=new String(d,0,6);System.out.println(s);/输出:Java你,3 字符串的加密算法(自学),使用一个字符串password作为密码对另一个字符串sourceString进行加密首先将password存放到一个字符数组里char p=password.toCharArray();明文中分为很多组,对每一组中的字符用数组a 的对应字符做加法运算。加密后:c0=(char)(a0+p0),c1=(char)(a1+p1),.,1正则表达式一个正则表达式是含有一些具有特殊意义字符的字符串,这些特殊字符称作正则
22、表达式中的元字符如“dcat”中的d就是由特殊意义的元字符,代表0-9中的任何一个。字符串0cat,1cat,2cat.9cat都是和正则表达式:dcat匹配字符串,9.1.6 正则表达式及字符串的替换与分解,字符串对象调用public boolean matches(String regex)方法可以判断当前字符串对象是否和参数regex指定的正则表达式匹配,元字符,说明,在正则表达式中可以用方括号括起来若个个字符来表示一个元字符,该元字符代表方括号中的任何一个字符。例如,regex=“159ABC”,那么“1ABC”,“5ABC”,”9ABC”都是和正则表达式regex匹配的字符串。更多内
23、容见P154,限定符,在正则表达式中可以使用限定符;如:X?,代表X出现0次或1次regex=“hello2468?”那么“hello”、“hello2”、“hello4”、“hello6”、“hello8”、都是与正则表达式regex匹配的字符串,限定符,例9-9,import;public class Example9_9 public static void main(String args)String regex=a-zA-Z“;Scanner scanner=new Scanner(System.in);String str=scanner.nextLine();if(str.ma
24、tches(regex)System.out.println(str+中的字符都是英文字母);,JDK1.4之后,字符串对象调用:public String replaceAll(String regex,String replacement)方法返回一个字符串,该字符串是当前字符串中所有和参数regex指定的正则表达式匹配的子字符串被参数replacement指定的字符串替换后的字符串,2.字符串的替换,举例,String result=12hello567.replaceAll(a-zA-Z+,你好);那么result的值就是“12你好567”,例10-10,public class Ex
25、ample9_10 public static void main(String args)String str=欢迎大家访问http:/了解、参观公司;String regex=(http:/|www)56?w+561w+561pAlpha+;(剔除n%sn中的网站链接信息后得到的字符串:n,str);str=str.replaceAll(regex,);System.out.println(str);,JDK1.4之后,String 类提供了一个实用的方法:public String split(String regex)字符串调用该方法时,使用参数指定的正则表达式regex做为分隔标记分
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 常用实用类 常用 实用 PPT 课件
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-5503510.html