1 / 20

第 7 章 学习目的、知识点、基本要求

第 7 章 学习目的、知识点、基本要求. 学习目的: 在掌握基本类库的基础上,初步学习使用 Java 的字符串类,为进一步学习奠定基础。 基本知识点: * 字符串的概念,字符串类与字符类型的区别; * String 类, String 类的构造方法; * String 类的主要成员方法的用途和用法; * StringBuffer 类,和 StringBuffer 类构造方法; * StringBuffer 类的主要成员方法的用途和用法。. 第 7 章 学习目的、知识点、基本要求. 基本要求: * 掌握字符串的概念,区别字符串类与字符类;

hal
Download Presentation

第 7 章 学习目的、知识点、基本要求

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第7章 学习目的、知识点、基本要求 学习目的: 在掌握基本类库的基础上,初步学习使用 Java的字符串类,为进一步学习奠定基础。 基本知识点: * 字符串的概念,字符串类与字符类型的区别; * String类, String类的构造方法; * String类的主要成员方法的用途和用法; * StringBuffer类,和StringBuffer类构造方法; * StringBuffer类的主要成员方法的用途和用法。

  2. 第7章 学习目的、知识点、基本要求 基本要求: * 掌握字符串的概念,区别字符串类与字符类; * 掌握String类的构造方法; *掌握String类的主要成员方法的用途和用法; *掌握StringBuffer类构造方法; *掌握StringBuffer类的主要成员方法的用途和 用法。

  3. 第7章 字符串类 7. 1 字符串与字符串类 7. 2 String串类的应用 7. 3 StringBuffer串类的应用 7. 4 字符串的应用

  4. 第7章 字符串类 Java.lang有两个字符串类 String和 StringBuffer,封装了字符串的全部操作。 • String用来处理创建以后不再改变的字符串; • StringBuffer用来处理可变字符串。 7. 1 字符串与字符串类 * 字符串常量:用双引号括起来的字符串。 * Java中的字符串都是以对象的形式出现的。 * Java所定义的字符串类的好处: ①任何系统平台都保证字符串本身及操作的一致性; ②字符串类的功能是可以预见的; ③字符串类被说明为最终类,以防用户对其修该; ④字符串类在运行时它们可以自动捕获异常。

  5. 字符串类的构造方法 String有9个构造方法,StringBuffer有3个构造方法。 例题5分析: import java.io.*; import java.applet.Applet; import java.awt.Graphics; public class StringDemo1 extends Applet { byte b[ ]={'A', ' ','b','y','t','e',' ','a','r','r','a','y'}; char c[ ]={'A',' ','c','h','a','r',' ','a','r','r','a','y'}; String s1,s2,s3,s4,s5,s6,s7,s8,s9; StringBuffer b1,b2,b3;

  6. 例题分析: public void init() { b1=new StringBuffer(); //创建一个空StringBuffer对象 b2=new StringBuffer(10); //创建一个长度为指定长度10的空对象 b3=new StringBuffer("A string buffer"); //以字符串为参数创建StringBuffer对象 s1=new String(); //创建一个空String对象 s2=new String("A string"); //以字符串为参数创建String对象 s3=new String(b3); //以StringBuffer对象参数创建String对象 s4=new String(b); //以b数组为参数创建String对象,8位字节自动转为16位字符 s5=new String(b,2,4); //从b数组的第3位开始创建有4个字符的对象 try{ s6=new String(b,2,10,"GBK");//同s5,最后的字符串参数为字符集编码 s7=new String(b,"GBK"); //同s5,最后的字符串参数为字符集编码 }catch(UnsupportedEncodingException e){} s8=new String(c); //以c字符数组为参数创建String对象 s9=new String(c,2,4); //从c字符数组第3开始创建有4个字符的对象 }

  7. 例题分析: public void paint(Graphics g) { g.drawString("s1="+s1,20,20); g.drawString("s2="+s2,20,35); g.drawString("s3="+s3,20,50); g.drawString("s4="+s4,20,65); g.drawString("s5="+s5,20,80); g.drawString("s6="+s6,20,95); g.drawString("s7="+s7,150,20); g.drawString("s8="+s8,150,35); g.drawString("s9="+s9,150,50); g.drawString("b1="+b1.toString(),150,65); g.drawString("b2="+b2.toString(),150,80); g.drawString("b3="+b3.toString(),150,95); } }

  8. 7.2 String串类的应用_1 1 求字符串长度 public int length() 可返回字符串长度。 例如: String s=“欢迎使用Java语言”; int len=s.length(); //len的值为10 注意: Java采用Unicode编码,每个字符为16位长,因此汉字和其他符号一样都只占一个字符。

  9. 7.2 String串类的应用_2 2 字符串连接 ①使用加号——‘+’连接字符串; ②public String concat(String str)可返回一个字符串,它将参数添加在原字符串的后边。 例如: ①String str1=“hello”; str1=str1+ “world !”; ② String str2=“to”.concat(“get”).concat(“her”); //str2 的值为together. 等价语句 Str=new StringBuffer().append(str1).append(“world!”).toString();

  10. 7.2 String串类的应用_3 3 字符串截取 ① 一次截取一个字符;charAt方法 格式:char charAt(int index) 功能:返回字符串index处的字符; index值从0到串长度减1。 例如:System.out.print(“Chaina”.charAt(1)); //输出为h

  11. 7.2 String串类的应用_3 3 字符串截取 ② 一次截取一个子串。substring方法 格式:String substring(int start) //start起始位置 Strint substring(int start, int end) //end结束位置 例如: String str=“a short string”; String s1=str.substring(2); // s1 为 short string String s2=str.substring(8,14); // s2 为 string 说明:字符串的起始位置从0开始

  12. 7.2 String串类的应用_4 4 字符串比较 ① equals方法:比较两个字符串是否相等 boolean equals(Object object) boolean equalsIgnoreCase(String str) ② compareTo方法:按字符顺序比较两个字符串 int compareTo(Object object) int compareTo(String str) int compareToIgnoreCase(String str) 例题: String s1=“This”; String s2=“That”; System.out.println(“结果1=”s1.equals(“this”)+ “,”+s1.equalsIgnoreCase(“this”)); System.out.println(“结果2=”s2.compareTo(“that”)+ “,”+s2.compareToIgnoreCase(“that”)); 屏幕输出: 结果1= false, true 结果2= -32,0 负数说明比较字符串That小于原字符串that ;结果为0表示比较的两字符串完全相同.

  13. 7.2 String串类的应用_5 5 拷贝到字符串 一个字符数组的内容可以全部或部分地拷贝到一个字符串中。 ① static String copyValueOf(char[]data) ② static String copyValueOf(char[]data, int offset, int count) 例如: char c[]={'A',' ','c','h','a','r',' ','a','r','r','a','y'}; String s1=new String(); String s2=new String(); s1=s1.copyValueOf(c); s2=s2.copyValueOf(c,2,4); s1的值为A char array, s2的值为char。

  14. 7.2 String串类的应用_6 6 字符串大小写转换 String s1=“all is lowercase”; String s2=“Some Is Uppercase”; s1=s1.toUpperCase(); s2=s2.toLowerCase(); 结果是: s1的字符串全部转换为大写, s2的字符串全部转换为小写。

  15. 7.2 String串类的应用_7 7 字符串检索 int indexOf(int ch) int indexOf(String str) int lastIndexOf(int ch) int lastIndexOf(String str) 例如: String str=“This is a test string”; int i=str.indexOf(‘i’); int j=str.lastIndexOf(‘i’); int k=str.indexOf(“is”); int l=str.lastIndexOf(“is”); 给出字符或字符串首次出现位置 给出字符或字符串最后一次出现的位置 结果: i为2, k为2, j为18, l为5

  16. 7.2 String串类的应用_8 8 字符串转换为数组 字符串可以转换为字节数组。 字符是16位长,而字节为8位长,所以要将字符的高8位去掉,只保留低8位成为一个字节。 使用如下三个方法进行转换: byte[] getBytes()//按系统默认字符集编码转换为字节数组 byte[] getBytes(String enc) //其中enc为字符集编码 char[] toCharArray() //转换为字符数组 例如: byte byteArr[]; char charArr[]; String str=“This is test string”; byteArr=str.getBytes(); //将字符串转换为字节数组 charArr=str.toCharArray(); //将字符串转换为字符数组

  17. 7.2 String串类的应用_9 9 转换为字符串 ValueOf方法将其他数据类型转换成字符串。 例如: char data[]={‘a’, ‘b’, ‘c’, ‘d’, ‘e’}; System.out.println(String.valueOf(12D)); System.out.println(String.valueOf(3<2)); System.out.println(String.valueOf(data,1,3)); 输出结果为: 12.0 false bcd

  18. 7.3 StringBuffer串类的应用 1 append方法   是用来将各种数据类型的数据转换为字符串后添加到当前字符串的尾部。 2 insert方法 是用来将需要插入的字符串插入到当前的字符串中 3 其他方法 ① 删除子串__delete ② 删除指定位置上的字符__deleteCharAt ③ 替换子串__replace ④ 翻转字符串__reverse

  19. 7.4 字符串类的应用_例题分析 输入一句话,统计其中有多少个单词。单词之间用空格分开。 import java.io.*; public class StringDemo2 { public static void main(String args[])throws IOException { InputStreamReader reader=new InputStreamReader(System.in); BufferedReader input=new BufferedReader(reader); System.out.print(“Enter:”); String st=input.readLine(); //从键盘输入字符行存入字符串的方法 int num=0; for(int i=0;i<st.lastIndexOf(‘ ’);i++) { i=st.indexOf(‘ ’,i); //确定最后一个空格位置 if(st.charAt(i+1)!=‘ ’) //保证截取下一个是字母而不是空格 num++; } if(st.indexOf(“ ”)!=0)num++; //索引 //当单词的数量不为0的时候会比总数少1,所以就要加1。 System.out.println(num+ “words”); } }

  20. 第7章 小结 字符是指用单引号括起来单个字符,Java中的字符是指占2个字节的Unicode字符;字符串指的是字符的序列,用双引号括起来的字符的序列都是字符串。 有两种类型的字符串:字符串常量和字符串变量。String类用于存储和处理字符串常量;StringBuffer类用于存储和操作字符串变量。

More Related