程序设计教学教案课件

上传人:aa****6 文档编号:54551339 上传时间:2018-09-14 格式:PPT 页数:77 大小:538.50KB
返回 下载 相关 举报
程序设计教学教案课件_第1页
第1页 / 共77页
程序设计教学教案课件_第2页
第2页 / 共77页
程序设计教学教案课件_第3页
第3页 / 共77页
程序设计教学教案课件_第4页
第4页 / 共77页
程序设计教学教案课件_第5页
第5页 / 共77页
点击查看更多>>
资源描述

《程序设计教学教案课件》由会员分享,可在线阅读,更多相关《程序设计教学教案课件(77页珍藏版)》请在金锄头文库上搜索。

1、第五章 常用实体类,5.1 String类,Java使用java.lang包中的String 类来创建一个字符串变量. 1.字符串常量如:”你好” “12.3456” “SCHOOL” 2.声明字符串String s;,5.1 String类,3.创建字符串 使用String类的构造方法例如:s=new String(“ we are students”); 也可以用一个已经创建的字符串去创建另一个字符串如:String tom=String(s); 另外比较常用的构造方法: String (char a):用一个字符数组a创建一个字符串对象,如:Char a=b , o , y;String

2、 s=new String(a); String(char a ,int startIndex , int count):提取字符数组a中的一部分字符创建一个字符串对象,参数startIndex和count分别指定在a中提取字符的起始位置和从该位置开始截取的字符个数.如:char a=s , t , b , u , s , n;String s=new String(a,2,3);,5.1 String类,4.引用字符串常量对象 字符串常量是对象,因此把字符串常量的引用赋值给一个字符串变量,则具有相同的实体. 如: String s1,s2; S1=“how are you”; S2=“how

3、 are you”;,0xAb28,0xAb28,How are you,5.1.5 String类的常用方法,1.public int length() 使用String 类的length()方法可以获取一个字符串的长度, 如: String s=“We are students”,tom=“我们是学生”; int n1,n2; n1=s.length(); n2=s.length();,5.1.5 String类的常用方法,2.public boolean equals(String s) 使用String 类的equals方法,比较当前字符串对象的实体是否与参数指定的字符串s的实体相同.

4、 如: String tom=new String(“we are students”); String boy=new String(“We are students”); String jerry=new String(“we are students”);tom.equals(boy) 的值是false tom.equals(jerry)的值是true字符串调用public boolean equalslgnoreCase(String s)比较当前字符串对象与参数指定的字符串s是否相同,比较时忽略大小写.,5.1.5 String类的常用方法,equals的用法 class Examp

5、le5_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); /输出结果是:true。System.out.println(s1=s2); /输出结果是:falseString s3,s4; s3=“how are you“;s4=“how are you“; System.out.println(s3.equals(s4); /输出结果是:true

6、。System.out.println(s3=s4); /输出结果是:true。 ,5.1.5 String类的常用方法,3.public boolean startsWith(String s) 、 public boolean endsWith(String s) 方法 字符串对象调用startsWith(String s)方法,判断当前字符串对象的前缀是否是参数指定的字符串s. 如: String tom=“220302620629021”; String jerry=“21079670924022”;tom.startsWith(“220”) 的值是true; jerry.starts

7、With(“220”)的值是false.使用endsWith(String s)方法,判断一个字符串的后缀是否是字符串s,如: tom.endsWith(“021”) 的值是true; jerry.startsWith(“021”)的值是false.,5.1.5 String类的常用方法,4.public boolean regionMatches(int firstStart,String other,int ortherStart,int length) 方法 字符串对象调用regionMatches(int firstStart,String other,int ortherStart,

8、int length) 方法,从当前字符串参数firstStart指定的位置开始处,取长度为length的一个子串,并将这个子串和参数other指定的一个子串进行比较,其中,other指定的子串是从otherStart指定的位置开始,从other中取长度为length的一个子串。如果两个子串相同该方法就返回true,否则返回false使用该方法的重载方法, public boolean regMatches (boolean b,int firstStart, String other , int otherStart ,int length) 可以通过参数b决定是否忽略大小写,当b取true

9、时,忽略大小写。,5.1.5 String类的常用方法,/判断一个字符串中出现了几个en class Example5_2 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); ,5.1.5 String类的常用方法,5.public int

10、 compareTo(String s) 方法 字符串对象可以使用compareTo(String s) 方法,按照字典序与参数s指定的字符串比较大小。如果当前的字符串与s,相同,该方法返回值是0;如果当前字符串对象大于s,该方法返回正值;如果小于s, 该方法返回负值。 如: String str=“abcde”;SpareTo(“boy”)小于0;pareTo(“aba”)大于0;pareTo(“abcde”)等于0按字典比较两个字符串还可以使用 public int compareTolgnoreCase(String s)方法,该方法忽略大小写,5.1.5 String类的常用方法,/字

11、符串排序 class Example5_3 public static void main(String args) String a=“boy“,“apple“,“Applet“,“girl“,“Hat“;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; for(int i=0;ia.length;i+) System.out.print(“ “+ai); ,5.1.5 String类的常用方法,6.public int indexOf(S

12、tring s) 方法 字符串对象调用indexOf(String s) 方法,从当前字符串的头开始检索字符串s,并返回首次出现s的位置。如果没有检索到字符串s,该方法返回的值是-1。 字符串调用indexOf(String s,int startpoint)方法从当前字符串的startpoint位置处开始检索字符串s,并返回首次出现s的位置。如果没有检索到字符串s,该方法返回的值是-1。 字符串调用lastindexOf(String s)方法从当前字符串的头开始检索字符串s,并返回最后出现s的位置,如果没有检索到字符s,该方法返回的值是-1。如: String tom=“I am a go

13、od cat”; tom.indexOf(“a”); /值是2 tom.indexOf(“good”,2); /值是7 tom.indexOf(“a”,7); /值是13 tom.indexOf(“w”,2); /值是-1,5.1.5 String类的常用方法,7.public String substring(int startpoint) 方法 字符串对象调用substring(int startpoint) 方法获得一个当前字符串的子串,该子串是从当前字符串的startpoint处截取到最后所得到的字符串,字符串对象调用substring(int start,int end)方法获得一个

14、当前字符串的子串,该子串从当前字符串的start处截取到end处所得到的字符串,但不包括end处所对应的字符。 如: String tom=“I love tom”; String s=tom.substring(2,5); /s值是lov,5.1.5 String类的常用方法,8.public String replaceAll(String old,String new) 方法 字符串对象s调用该方法可以获得一个串对象,这个串对象是通过参数new指定的字符串替换s中的由old指定的所有字符串而得到的字符串。,5.1.5 String类的常用方法,9.public String trim()

15、 方法 字符串s调用该方法可以得到一个字符串对象,该字符串是s去掉前后空格后的字符串。,5.1.6 字符串与基本数据类型的相互转化,java.lang包中的Integer类调用其方法: public static int parseInt(String s) 可以将“数字”格式的字符串转化为int型数据; 如: int x; String s=“3452”; x=Integet.parseInt(s);,5.1.6 字符串与基本数据类型的相互转化,java.lang包中的Byte、Short、Long、Float、Double类的数据类型转化方法: public static byte par

16、seByte(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),5.1.6 字符串与基本数据类型的相互转化,/从键盘输入若干个数,求平均值 public class Example5_4 public static void main(String args) double n,sum=0.0 ;for(int i=0;iargs.length;i+) sum=sum+Double.parseDouble(argsi);n=sum/args.length;System.out.println(“平均数:“+n); /*编译后,使用控制台输入数值 *java Example5_4 “20” “67.05” “12.66” “20.1” */,

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

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