第7章常用实用类概要

上传人:今*** 文档编号:108301005 上传时间:2019-10-23 格式:PPT 页数:63 大小:610KB
返回 下载 相关 举报
第7章常用实用类概要_第1页
第1页 / 共63页
第7章常用实用类概要_第2页
第2页 / 共63页
第7章常用实用类概要_第3页
第3页 / 共63页
第7章常用实用类概要_第4页
第4页 / 共63页
第7章常用实用类概要_第5页
第5页 / 共63页
点击查看更多>>
资源描述

《第7章常用实用类概要》由会员分享,可在线阅读,更多相关《第7章常用实用类概要(63页珍藏版)》请在金锄头文库上搜索。

1、第七章 常用实用类,7.1 String类 7.2 StringBuffer类 7.3 StringTokenizer类 7.4 Date类 7.5 Calendar类 7.6 Math类 7.7 BigInteger类 7.8 模式类,7.1 String类,1、 字符串常量 如:“你好”,“1234.987”等 2、 声明字符串 String s; 3、 创建字符串 (1)使用 String 类的构造方法 String s=new String(“we are students”); (2)用一个已创建的字符串创建另一个字符串: String tom=String(s);,7.1 Stri

2、ng类,String 类还有两个较常用构造方法: (1)String (char a ) 例如: char a3=b,o,y; String s=new String(a); (2)String(char a,int startIndex,int count) 例如: char a=s,o,k,a,b; String s=new String(a,1,2);,7.1 String类,4、引用字符串常量 String s1,s2; s1=“how are you”; s2=“how are you”; s1和s2具有相同的引用,因而具有相同的实体。,7.1 String类,5、字符串的常用方法

3、(1)public int length() 用于获取字符串长度。 如: String tom= “我们是学生“; int n1,n2; n1=tom.length(); n2=“你好”.length();,7.1 String类,(2) public boolean equals(String s) 如 : String tom=new String( “how are you“); String boy=new String( “How are you “); String jerry= new String(“how are you“); tom.equals(boy)的值是 false

4、, tom.equals(jerry)的值是 true. 注意: tome=jerry的值为false。 public boolean equalIgnoreCase(String s),例7.1 class Example7_1 public static void main(String args) String s1,s2; s1=new String(“we are students“); s2=new String(“we are students“); System.out.println(s1.equals(s2); System.out.println(s1=s2); Stri

5、ng s3,s4; s3=“how are you“; s4=“how are you“; System.out.println(s3.equals(s4); System.out.println(s3=s4); ,7.1 String类,(3)public boolean srartsWith(String s) String tom= “220302620629021“, jerry=“21079670924022“; tom.startsWith(“220”)的值是 true, jerry.startsWith(“220“)的值是 false. public boolean endsWi

6、th(String s) tom.endsWith(“021“)的值是 true, jerry.endsWith(“021“)的值是 false.,7.1 String类,(4)public boolean regionMatches(int firstStart, String other,int ortherStart,int length) public boolean regionMatches(boolean b, int firstStart,String other,int ortherStart, int length),7.1 String类,class Example7_2

7、 public static void main(String args) int number=0; String s=“student;entropy;engage,english,client“; for(int k=0;ks.length();k+) if(s.regionMatches(k,“en“,0,2) number+; System.out.println(“number=“+number); ,7.1 String类,(5) public int compareTo(String s) public int compareToIgnoreCase(String s) 例如:

8、 String str= “abcde“ pareTo(“boy”)小于,值小于0。 pareTo(“aba”)大于,值大于0。 pareTo(“abcde”)等于0。,7.1 String类,class SortString public static void sort(String a) for(int i=0;ia.length-1;i+) for(int j=i+1;ja.length;j+) if(pareTo(ai)0) String temp=ai; ai=aj; aj=temp; ,7.1 String类,class Example7_3 public static void

9、 main(String args) String a=“door“,“apple“,“Applet“,“girl“,“boy“; SortString.sort(a); for(int i=0;ia.length;i+) System.out.print(“ “+ai); ,7.1 String类,(6)public int indexOf(String s) public int indexOf(String s,int startpoint) public int lastIndexOf(String s) public int lastIndexOf(String s,int star

10、tpoint) 例如: String tom=“I am a good cat”; tom.indexOf(“a”); tom.indexOf(“good”,2); tom.indexOf(“a”,7); tom.indexOf(“w”,2);,7.1 String类,(7) public String substring(int startpoint) public String substring(int start,int end) 例如: String tom=“I love tom”; String s=tom.substring(2,5); (8)public String rep

11、laceAll(String old,String new) public String replaceFirst(String old,String new) 例如: String s= “I mist theep “; String temp=s.replace(t,s);,7.1 String类,(9)public String trim() 例7.4 class Example7_4 public static void main(String args) String path=“C:bookjavabookxml.doc“; String newPath=path.replaceA

12、ll(“.txt“,“.java“); System.out.println(path); System.out.println(newPath); int index=path.lastIndexOf(“); String fileName=path.substring(index+1); System.out.println(fileName); ,7.1 String类,6、字符串与基本数据的相互转化 (1)字符串转化为基本整型 public static int parseInt(String s) 例如: int x; String s=“6542“; x=Integer.parse

13、Int(“6542“);,7.1 String类,字符串转化为基本整型的其他转换方法: public static byte parseByte(String s) public static short parseShort(String s) public static long parseLong(String s) public static float parseFloat(String s) public static double parseDouble(String s),7.1 String类,(2)数值转化为字符串 public static String valueOf(

14、byte n) public 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 等数值转化为字符串,如: String str=String.valueOf(12313.9876);,7.1 String类,可以使用Long类中的下列方法得到整数的各进制的字符串表示: public static String toBinary

15、String(long i) public static String toOctalString(long i) public static String toHexString(long i) public static String toString(long i,int p),7.1 String类,class Example7_5 public static void main(String args) double n,sum=0,item=0; boolean computable=true; for(int i=0;iargs.length;i+) try item=Double.parseDouble(argsi); sum=sum+item; catch(NumberFormatException e) System.out.println(“您键入了非数字字符:“+e); computable=false; ,7.1 String类,if(computable) n=sum/args.length; System.out.println(“平均数:“+n); int number=8642; String binaryString=Long.toBinaryString(number); System.out.println(number+“的二进制

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号