1 / 21

第 8 章 字符串

第 8 章 字符串. Java 程序设计 教程. 本章主要内容. 创建字符串 使用 String 类 使用 StringBuffer 类. 字符. 字符和字符串的基础知识. 字符是 构造 Java 源程序的基本元素 。 表示方法为:用单引号括起来。 例如,‘ Z ’ 、‘ <br> ’ 代表换行符。. 字符串. 字符和字符串的基础知识. 字符串是作为一个单元来进行处理的一系列字符,由字母、数字和各种特殊字符组成 。 表示方法为:双引号括起来的字符序列 &quot;this is a java string&quot;

thyra
Download Presentation

第 8 章 字符串

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. 第8章 字符串 Java程序设计教程

  2. 本章主要内容 • 创建字符串 • 使用String类 • 使用StringBuffer类

  3. 字符 字符和字符串的基础知识 • 字符是构造Java源程序的基本元素。 • 表示方法为:用单引号括起来。 • 例如,‘Z’ 、‘\n’代表换行符。

  4. 字符串 字符和字符串的基础知识 • 字符串是作为一个单元来进行处理的一系列字符,由字母、数字和各种特殊字符组成 。 • 表示方法为:双引号括起来的字符序列 • "this is a java string" • "it contains a \" " //此字符串有含有特殊字符\" 字符与字符串的区分在于所使用的表示符号。

  5. 创建String对象 创建字符串对象 • 创建方法1:String str1="This is a string"; • 每个用双引号括起的字符串都作为String类的一个实例 • 创建方法2:String str3=new String(str1) ; • 创建方法3:由字符数组来产生 • char ch[]={'a', 'b', 'c'}; • String str4=new String(ch); • 其它创建String对象方法的方法见例8.1

  6. 创建StringBuffer对象 创建字符串对象 • 利用StringBuffer类的3个构造方法创建StringBuffer类的对象。 • public StringBuffer() • 构造一个没有字符的字符串缓冲区,初始容量为16个字符长。 • public StringBuffer(int length) • 构造一个没有字符的字符串缓冲区,初始容量由参数length指定。 • public StringBuffer(String string ) • 给参数string构造一个字符串缓冲区,初始容量为string长度加16

  7. 例8.1 字符串对象的创建 创建字符串对象 import java.io.*; import java.applet.Applet; import java.awt.Graphics; public class StringDemo 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; public void init() { b1=new StringBuffer(); b2=new StringBuffer(10); b3=new StringBuffer("A string buffer");

  8. 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个元素为参数创建String对象 try{ //如果下面的字符集编码不存在将抛出异常 s6=new String(b,2,10,"GBK"); //同s5,最后的字符串参数为字符集编码 s7=new String(b,"GBK"); //同s4,最后的字符串参数为字符集编码 }catch(UnsupportedEncodingException e){} s8=new String(c); //以字符数组c为参数创建String对象 s9=new String(c,2,4); //从c的第3位,取4个元素为参数创建String对象 }

  9. 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); } }

  10. 例8.1的结果

  11. String 类 • 类String是用来表示字符串一般用途的类,它的特性是一旦字符串产生之后,字符串的内容就不再变动。 • String类的方法

  12. 求字符串长度: • public int length():它返回一个字符串的长度。 • 例如: • String message="abcdefg"; • System.out.print(message.length()); • 输出字符串message的长度7。 • 连接字符串 • 使用串连接操作符 “+” 将两个字符串连接起来 " and " + " html " ; 结果为" andhtml" • 可以用concat()方法连接两个字符串 public void concat(String s) s1="abc",s2="def " ; String s3=s1.concat(s2); s3 的结果:abcdef String类的方法

  13. String类的方法 • 比较字符串 • boolean equals(String str) • boolean equalsIgnoreCase(String str)//忽略字符大小写 • int compareTo(String str) • int compareToIgnoreCase(String str) //忽略字符大小写 例如: String s1="this"; String s2="that"; System.out.println(s1.equals("This")); System.out.println(s2.equalsIgnoreCase("This")); System.out.println(s1.compareTo("That")); System.out.println(s2.compareToIgnoreCase("That")); 输出结果为: false true 32 0

  14. String类的方法 • 搜索(截取)字符:指一次取得一个字符串 中的某一个字符,通过charAt方法实现。 public char charAt(index) 例如:String message="Welcome to java"; message.charAt(0); 结果为: 'W' 取得字符串中指定的字符,index的范围是从0到字符串的长度减1。

  15. String类的方法 • 修改字符串 :用replace()方法,它负责把一个字符替换成为另外一个字符。 例如:String message="Welcome to jaua"; System.out.println(message.replace('u', 'v')); System.out.println(message.replace('a', 'e')); 输出的结果为: Welcome to java Welcome to jeve

  16. String类的方法 表8-1 String类的其它部分方法

  17. 使用StringBuffer类 • StringBuffer类的字符串内容是“可改变的”,可以直接改变字符串本身,而不是像String类那样产生另一个新的对象实例作为字符串处理的结果。 • StringBuffer类不提供一般性的字符串处理方法,而是提供了有关字符串的改变、增长和插入……等和String类不同的方法。

  18. StringBuffer类的方法 • 把字符串添加到缓冲区:对于字符串缓冲区,可以其末尾追加新内容。 • public StringBuffer append(数据类型) • 把字符串插入到缓冲区 :在指定位置插入新内容的方法 。 • public StringBuffer insert(插入位置,数据类型) • 其中插入位置参数一定大于等0 • 注意:每个字符串缓冲区都有一个容量。如果超过它的容量,缓冲区会自动加大,以存放新加的字符。 数据类型是指除byte类型外的所有类型。

  19. StringBuffer类的方法 • 从缓冲区中获取字符:这个方法返回字符串缓冲区中指定位置的字符。 public char charAt(int index) 字符串缓冲区的下标从0开始。 • 求缓冲区容量和字符串: • 求容量:public int capacity(),返回字符串缓冲区的当前容量。(容量是指可以存入字符串缓冲区的字符数) • 输出字符串内容:public String toString(),返回与所引用对象相等的String。

  20. StringBuffer类的方法 • 修改缓冲区中字符串 • public StringBuffer delete(int start ,int end):删除从start 开始到end为止的子串 • public StringBuffer deleteCharAt(int index):删除index所指定位置上的字符。 • public StringBuffer replace(int start,int end,String str):用子串str替换从start 开始到end为止的子串。 • StringBuffer reverse():反转字符串缓冲区的字符串。 • public void setCharAt(int index,char ch):将字符串缓冲区指定位置的字符设置为ch。

  21. StringBuffer类的方法 StringBuffer类几个方法的例子 语句段: StringBuffer strBuf=new StringBuffer("Welcome to java"); strBuf=strBuf.delete(11,14); strBuf=strBuf.deleteCharAt(8); strBuf=strBuf.insert(8, "o"); strBuf=strBuf.append("html"); strBuf=strBuf.setCharAt(8, 't'); strBuf=strBuf.replace(0,1, "We"); strBuf=strBuf.reverse(); 结果为: Welcome to Welcome o Welcome oo Welcome oo html Welcome to html We Welcome to html Lmth ot emocleW eW 结果:

More Related