1 / 42

Java 程序设计基础

Java 程序设计基础. 第 4 章 字符串. www.avceit.cn. 1. String 字符串类. 2. StringBuffer 字符串类. 3. 命令行参数. Contents. 第 4 章 字 符 串.

gail
Download Presentation

Java 程序设计基础

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. Java程序设计基础 第4章 字符串 www.avceit.cn

  2. 1 String 字符串类 2 StringBuffer字符串类 3 命令行参数 Contents

  3. 第4章 字 符 串 • 字符串是字符的序列,它是组织字符的基本数据结构,对于大多数程序来说都是很重要的,即使在图形程序中也需要对字符串(文件名和其他对象名)进行处理。字符串中可以包含字母、数字和其他各种特殊字符,如+、-、*、/等。在有些语言中(如C语言),字符串就是用字符数组来实现的,而在Java中是将字符串当作对象来处理,它提供了一系列的字符串操作方法,使字符串的处理更加容易和规范。 • Java语言中的包java.lang中封装了final类String和StringBuffer,其中类String对象是字符串常量,建立后不能改变。而类StringBuffer对象类似于一个字符缓冲区,建立后可以修改。这两个类共同的特点都不能再派生子类,即它们不能被其他类继承。

  4. 4.1 String 字符串类 • 首先要清楚的是,字符串常量和字符常量的区别。字符常量是用单引号括起来的单个字符,如’a’,’3’,’\052’等。而字符串常量是用双引号括起来的字符序列,如"a","java","\n"等。在Java中,每个字符串常量对应一个String类的对象,所以一个字符串常量可以直接调用类String中提供的方法,例如: • int len= "hello".length(); • 将得到字符串的长度5,字符串的长度即字符串中字符的个数。 • 本节主要讨论存放字符串常量的String类,包括String对象的创建、使用和操作。

  5. 4.1.1 类String字符串的创建 • String类是字符串常量类,String对象建立后不能修改。以前使用的每个字符串常量实际上都是String对象,如字符串“hello”在编译后即成为String对象。因此,可以用字符串常量直接初始化一个String对象。 • 例如: • String s="Java Program";

  6. 通过类String提供的默认的构造方法不需要任何参数,它生成一个空字符串。如:通过类String提供的默认的构造方法不需要任何参数,它生成一个空字符串。如: • Sting s=new String();//建立一个空字符串对象 • 通过使用类String提供的构造方法,除了可以生成一个空字符串,还可以用字符数组或字节数组来生成一个字符串对象。其他创建String对象的构造方法如表6-1所示。

  7. 注意: • 由于在Internet上通常使用的字符都为8位的ASCII码,Java提供了从字节数组来初始化字符串的方法,并且用hiByte来指定每个字符的高位字节。对ASCII码来说,hiByte应为0,对于其他非拉丁字符集,hiByte的值应该非0。

  8. 【 例4.1】 String构造方法的使用。 • public class UseConstructors{ • public static void main(String args[]){ • String s1,s2,s3,s4,s5,s6,s7; • byte b[]={(byte)'J',(byte)'a',(byte)'v',(byte)'a'}; • char c[]={'基','础','教','程'}; • StringBuffer sb =new StringBuffer("你好"); • s1=new String(); • s2=new String("Hello"); • s3=new String(sb); • s4=new String(c); • s5=new String(c,0,2); • s6=new String(b,0); • s7=new String(b,0,1,2); System.out.println("s1="+s1); System.out.println("s2="+s2); System.out.println("s3="+s3); System.out.println("s4="+s4); System.out.println("s5="+s5); System.out.println("s6="+s6); System.out.println("s7="+s7); } }

  9. 4.1.2 类String字符串处理的常用方法 • 类String中提供的访问String字符串的方法很多,大体上可分为比较、判断前后缀、单个字符的查找、子字符串定位、连接和类转换等几类。 • 1.类String字符串的比较 • (1)boolean equals(Object anObject)和boolean equalsIgnoreCase(String anString) • 功能:用来比较两个字符串的值是否相等,不同的是后者忽略字母的大小写。例如下面的表达式: • "java".equals("java") //值为true • "java".equalsIgnoreCase("JAVA") //值为true • 注意:它们与运算符“==“实现的比较是不同的。运算符“==“比较两个字符串对象是否引用同一个实例对象,而equals()和equalsIgnoreCase()则比较两个字符串中对应的每个字符是否相同。

  10. 例如: • String s1= new String("java"); • String s2= new String("java"); • String s3=s1; • System.out.println(s1==s2); • System.out.println("java"=="java"); • System.out.println(s1==s3); System.out.println(s1.equals(s2));

  11. (2)int compareTo(String anString) 和int compareToIgnoreCase(String str) • 功能:用于比较两个字符串的大小,所不同的是后者忽略字母大小写。通过返回的整数值指明当前字符串与参数字符串的大小关系。若调用串比参数串大,返回正整数;反之,返回负整数;相等则返回0。 • 比较方法: • 若比较的两个字符串有不同的字符,则从左边数起的第一个不同字符的大小即两个字符串的大小,字符的大小建立在Unicode字符集基础上,方法的返回值为: • 调用串中的第一个不同字符-参数串中的第一个不同字符。 • 例如: • "this".compareTo("that") //值为8;因为i-a=8 • 若比较的两个字符串各个位置的字符都相同,仅长度不同,则方法的返回值为: • 调用串的长度-参数串的长度 • 例如: • "abcd".compareTo("abc") //值为4-3=1

  12. (3)boolean regionMatches(int toffset,String other,int ooffset,int len)和 boolean regionMatches(Boolean ignoreCase,int toffset,String other,int ooffset,int len); • 功能:用于比较两个字符串中指定区域的子字符串是否相同。 • 其中,toffset和ooffset分别指明当前字符串和参数字符串中所要比较的子字符串的起始位置,len指明比较的字符个数(长度),而ignoreCase指明比较时是否区分大小写,若无此参数,比较是区分大小写的。 • 例如: • "Java".regionMatches(0,"java",0,4) //false • "Java".regionMatches(true,0,"java",0,4) //true

  13. 2.判断字符串的前缀和后缀 • boolean startsWith(String prefix)和 • boolean endsWith(String suffix) • 用于判断当前字符串的前缀和后缀是否和指定的字符(或字符串)相同。如: • 居民身份证的最后一位数字表示性别,奇数为男性,偶数为女性,则可通过下面的语句来辨别某位居民的性别: • if(s.endsWith("1")||s.endsWith("3")||s.endsWith("5") ||s.endsWith("7")||s.endsWith("9")) • {System.out.println("此人为男性");} • else {System.out.println("此人为女性");}

  14. 3.字符串中单个字符的查找 • (1)char charAt(int index) • 功能:返回字符串在index处的字符,index的值从0到串长度减1。例如: • System.out.println("Java".charAt(1)); //输出为a • (2)int indexOf(int ch); 和 • int indexOf(int ch,int fromIndex) • 功能:返回当前字符串中某特定字符ch出现的位置。第1种方法从前向后顺序查找, • 第2种方法从指定位置fromIndex之后开始查找,若找到,则返回该字符第1次出现的位置序号,否则返回-1。

  15. 【 例4.2】 indexOf的使用 • class UseIndexOf{ • public static void main(String args[]){ • String s="java program language"; • int i=-1; • do{ • i=s.indexOf((int)'g',i+1); • System.out.print(i+"\t"); • }while(i!=-1); • } • } • 运行结果: • 8 16 19 -1

  16. 4.类String子字符串的定位 • 1.int indexOf(String str)和 • int indexOf(String str,int fromIndex) • int lastIndexOf(String str)和 • int lastIndexOf (String str,int fromIndex) • 方法indexOf()的功能是从前向后查找指定子字符串在主串中出现的位置。 • 方法lastIndexOf()的功能是从后向前查找指定子字符串在主串中出现的位置。

  17. 例4.3 lastIndexOf的使用 • class UseLastIndexOf{ • public static void main(String args[]){ • String s="java program language"; • int i=s.length(); • do{ • i=s.lastIndexOf("g",i-1); • System.out.print(i+"\t"); • }while(i!=-1); • } • } • 运行结果: • 19 16 8 -1

  18. 2. String substring(int beginindex) • String substring(int beginindex,int endindex) • 功能:返回子字符串。前者从主串的beginindex处开始到串尾获取一个子串返回;后者从主串的beginindex处开始到endindex-1处获取一个子串返回,子串的长度=endindex-beginindex。

  19. 【 例4.4】 substring方法的使用 • class UseSubstring • { • public static void main(String args[]) • { • String s1="java program language"; • int beginindex=s1.indexOf("p"); • int endindex=s1.lastIndexOf("m"); • String s2=s1.substring(beginindex,endindex+1); • System.out.println(s2); • } • } • 运行结果: • program

  20. 5.字符串的连接 • String concat(String str); • 功能:将str连接到主串的末尾,并返回连接后的字符串,但是主串本身并没有改变。 • 如下面的例子: • String s="java"; • System.out.println(s.concat("程序设计"); • System.out.println(s); • 运行结果: • java程序设计 • java • 注意:在java中,重载的运算符"+"也可以实现字符串的连接或字符串与其他类对象的连接。

  21. 6.String类的转换 • (1)static String valueOf(Object obj) • 功能:将其他类的参数转换为字符串对象并返回。由于Object类是所有类的父类,所以此方法是重载的,参数可以是java中的任何类型。 • 例如: • double x=3.1415926; • String s=String.valueOf(x); //将double类型转换成String类的对象 • 注意:其他类也提供了方法valueOf(),用来将一个字符串转换成相应类的对象。 • 例如: • String s="3.1415926"; • Double d=Double.valueOf(s); //将String类的对象转换成Double类的对象 • double y=d.doubleValue(); //将Double类的对象转换成double类型的数据

  22. (2)toString() • 该方法的功能是把其他类的对象转换成字符串对象。 • 如: • String s=Integer.toString(34,16); //将十进制数34转换成16进制字符串 • System.out.println(s); //输出为22 • (3)toCharArray() • 该方法的功能是将字符串转换成一个字符数组。 • 如: • String s1="We like orange"; • char s2[]=s1.toCharArray(); • System.out.println(String.valueOf(s2,8,6); • 输出结果: • orange

  23. 7.类String的其他方法 • (1)字符串中字符大小写的转换 • 可以利用String类提供的下列方法进行转换。 • ①public String toLowerCase() • 该方法将字符串中所有字符转换成小写,并返回转换后的新串。 • ②public String toUpperCase() • 该方法将字符串中所有字符转换成大写,并返回转换后的新串。 • (2)去除字符串中多余空格 • public String trim() • 该方法只是去掉开头和结尾的空格,并返回得到的新字符串。值得注意的是,在字符串中间的空格并不会被去掉。

  24. (3)字符串中字符的替换 • ①public String replace(char oldChar,char newChar) • 该方法用字符newChar替换当前字符串中所有是oldChar的字符,返回一个新字符串。 • ②public String replaceFirst(String regex, String replacement) • 该方法用字符串replacement的内容替换当前字符串中遇到的第一个和字符串regex相一致的子串,并将产生的新字符串返回。 • ③public String replaceAll(String regex, String replacement) • 该方法用字符串replacement的内容替换当前字符串中所有和字符串regex相一致的子串,并将产生的新字符串返回。

  25. 4.2 StringBuffer字符串类 • 与实现字符串常量的String类不同的是,StringBuffer类的每个对象都是可以扩充和修改的字符串变量,使用起来比String类更加方便。 • 本节主要讨论存放字符串变量的StringBuffer类,包括StringBuffer对象的创建、使用和修改操作。

  26. 4.2.1 类StringBuffer字符串的创建

  27. 4.2.2 类StringBuffer字符串的常用方法 • 1.字符设置和替换 • (1)void setCharAt(int index,char ch) • 功能:设置指定位置index的字符值为ch • 如:StringBuffer str=new StringBuffer("stident"); • str.setCharAt(2, ‘u’) //将str字符串中的第二个字符替换为u • (2) StringBuffer replace(int start,int end,String str) • 功能:将StringBuffer对象字符串从start至end-1处,用字符串str来替换 • 如: StringBuffer str=new StringBuffer("happy new year"); • System.out.println(str.replace(6,14, "birthday")); • 输出结果 : • happy birthday

  28. 2.设置/获取类StringBuffer字符串对象长度和容量 • (1)void setLength(int newLength) • 功能:设置字符串的长度为newLength,这时字符串缓冲区中指定长度以后的字符值均为零 • (2) int length() • 功能:获取字符串对象的长度 • (3)void ensureCapacity(int minimumCapacity) • 功能:设定字符串缓冲区的大小,保证缓冲区的容量至少为指定的值minimumCapacity,若当前缓冲区容量小于参数值,则分配新的较大的缓冲区,新容量取以下两者中的大者: • •参数minimumCapacity的值 • •老容量的两倍加2 • (4)int capacity()

  29. 【 例4.5】 StringBuffer字符串长度Length和容量Capacity的设置和获取 • public class LengthAndCapacity • { • public static void main(String args[]) • { • StringBuffer sb1=new StringBuffer(); • StringBuffer sb2=new StringBuffer(30); • StringBuffer sb3=new StringBuffer("abcde"); • System.out.println("sb1.capacity="+sb1.capacity()); • System.out.println("sb1.length="+sb1.length()); • System.out.println("sb2.capacity="+sb2.capacity()); • System.out.println("sb2.length="+sb2.length()); • System.out.println("sb3.capacity="+sb3.capacity()); • System.out.println("sb3.length="+sb3.length());

  30. sb2.ensureCapacity(50); • sb3.setLength(3); • System.out.println("sb2.capacity="+sb2.capacity()); • System.out.println("sb2.length="+sb2.length()); • System.out.println("sb3.capacity="+sb3.capacity()); • System.out.println("sb3.length="+sb3.length()); • } • }

  31. 【 例4.6】在StringBuffer字符串末尾添加内容 • public class StringBufferAppend • { • public static void main(String args[]) • { • StringBuffer sb=new StringBuffer("happy"); • sb.append("new"); • sb.append("year"); • System.out.println(sb); • System.out.println("sb.capacity="+sb.capacity()); • System.out.println("sb.length="+sb.length()); • } • } 运行结果: happynewyear sb.capacity=21 sb.length=12

  32. (2)StringBuffer insert(int offset,char ch) • 功能:在字符串的指定位置offset处插入字符ch • 【 例4.7】 StringBuffer字符串的insert方法的使用 • public class UseInsert • { • public static void main(String args[]) • { • StringBuffer sb=new StringBuffer("world"); • sb.insert(0,"hello"); • System.out.println(sb); • } • } 运行结果: helloworld

  33. 【例4.8】 对StringBuffer字符串进行颠倒操作 • public class UseReverse • { • public static void main(String args[]) • { • StringBuffer sb1=new StringBuffer("abcde"); • StringBuffer sb2=sb1.reverse(); • System.out.println(sb1); • System.out.println(sb2); • } • } 运行结果: edcba edcba

  34. 4.获取类StringBuffer对象的子字符串或字符 • (1)void getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin) • 功能:将StringBuffer对象字符串中字符复制到目标字符数组dst中去。复制的字符从srcBegin处开始,到srcEnd-1处结束,复制字符的个数为srcEnd-srcBegin,字符被复制到目标数组dst的dstBegin至dstBegin+(srcEnd-srcBegin)-1处 • 如:char c[]={"***********"}; • StringBuffer sb=new StringBuffer("hello"); • sb.getChars(0,5,c,3); • System.out.println(c); //输出***hello***

  35. (2)String substring (int start)和String substring(int start,int end) • 功能:获取StringBuffer字符串对象从start处开始的子串;获取StringBuffer字符串对象从start开始到end-1处的子串。 • 如: • StringBuffer sb=new StringBuffer("java program"); • System.out.println(sb.substring(5)); //输出program • System.out.println(sb.substring(0,4)); //输出java

  36. 5.类型转换 • 类StringBuffer重写了方法tostring(),可将类StringBuffer的对象转换成类string的对象.如: • StringBuffer sb=new StringBuffer("java"); • String s=sb.toString(); • System.out.println(s);

  37. 4.3 命令行参数 • 在main()方法名后的括号中有String类的数组参数args,是用来接收Java命令行传送给java应用程序的数据。命令行参数就是在执行字节码文件时,命令行上字节码文件名后给出的内容。例如: • java UseString hello java world • 其中:UseString是字节码文件名,hello java world就是命令行参数 • Java通过args字符串数组来得到并处理这些内容,命令行参数有多个时,要用空格隔开,这些参数将按顺序逐个存入args数组中,第一个参数hello存入args[0],第二个参数java存入args[1],第三个参数world存入args[2]。 • 注:要想将包含有多个空格的单词作为一个字符串时,可用引号括起来。如: • java UseString "hello java world"

  38. 【 例4.9】 • public class Command • { • public static void main(String args[]) • { • for(int i=0;i<args.length;i++) • {System.out.println("args["+i+"]="+args[i]);} • } • } • 若执行程序的命令为: • java Command hello java • 则程序运行结果为: • args[0]=hello • args[1]=java

  39. 例4.10】用命令行参数方式提供两个整数,把它们相乘后输出。 • public class UseComLParam{ • public static void main(String args[]){ • int s,s1,s2; • if(args.length<2) • { • System.out.println("运行本程序需提供两个命令行参数"); • System.exit(0); • } • s1=Integer.parseInt(args[0]); • s2=Integer.parseInt(args[1]); • s=s1*s2; • System.out.println(s1+"*"+s2+"="+s); • } • } 执行该程序的过程如下: 编译:javac UseComLParam.java 运行:java UseComLParam 3 4 运行结果: 3*4=12

  40. 本章小结 • 通过本章的学习了解Java中字符串类的基本概念,掌握字符串的一些常用方法及其使用。字符串指的是字符的序列,用双引号括起来的字符的序列都是字符串。 • 在Java有两种类型的字符串:一种是创建以后不需要改变的,称为字符串常量,在Java中,String类用于存储和处理字符串常量;另外一种字符串是创建以后,需要对其进行改变的,称为字符串变量,在Java中,StringBuffer类用于存储和操作字符串变量。 • 通过实验训练,掌握两种字符串的一些常用的方法来解决Java中的一些文字问题,将十分方便。 • 字符串可以作为main()方法的参数,传递给应用程序,利用String args[]数组来进行操作,输入时放在命令行字节码文件名的后面,称为“命令行参数”,它是向Java Application程序传递参数数据的常用而且有效的手段。

  41. Thank You ! www.avceit.cn

More Related