java字符串和正则表达式.ppt
《java字符串和正则表达式.ppt》由会员分享,可在线阅读,更多相关《java字符串和正则表达式.ppt(30页珍藏版)》请在三一办公上搜索。
1、第5章 字符串和正则表达式,本章学习目标,掌握字符串的常用属性及方法掌握String类和StringBuffer类的使用了解StringTokenizer类的使用了解正责表达式的使用了解Pattern和Matcher类的使用,5.1 字符串常量,Java语言规定字符串常量必须用双引号括起,一个串可以包含字母、数字和各种特殊字符,如+、-、*、/、$等。例如:”You are student.”。当字符串中包含一个字符时,不要把它和字符常量混淆,例如:a是字符常量,而”a”是字符串常量。,实例5-1 字符串常量使用示例,class Exam5_1public static void main(S
2、tring args)System.out.println(This is constant string!);System.out.println(String+connection.);int i=10;System.out.println(i=+i);,5.2 String类字符串,String类是Java处理字符串的标准格式,也是表示定长字符串的常用方式。String对象一旦被创建了,就不能被改变。如果需要大量改变字符串的内容,应该考虑使用StringBuffer类或者字符数组,其最终结果可以被转换成String格式。,实例5-2多种方法创建String类对象,class Exam5_
3、2 public static void main(String args)String s1=hello;/使用字符串常量赋值 String s2=new String();/通过构造方法创建对象再赋值 s2=hello;String s3=new String(hello);/利用构造方法直接赋值 char s=h,e,l,l,o;/利用字符数组型创建字符串 String s4=new String(s);byte b=104,101,108,108,111;/字节数组型的字符串,参数分别是hello每个字母的ASCII码 String s5=new String(b);System.ou
4、t.println(s1=+s1);System.out.println(s2=+s2);System.out.println(s3=+s3);System.out.println(s4=+s4);System.out.println(s5=+s5);,5.2.2 String类字符串的基本操作,String类提供了多种方法,使用这些方法或方法的组合,可方便地实现对字符串的处理,实例5-3 String类常用方法示例,class Exam5_3 public static void main(String args)String s=hello;System.out.println(s);Sy
5、stem.out.println(this string contains+s.length();System.out.println(the charracter index 4 is+s.charAt(4);System.out.println(the index of the character e is+s.indexOf(e);System.out.println(the hash code for this string is+s.hashCode();,实例5-4 String类求子串示例,class Exam5_4 public static void main(String
6、args)String s=hello;System.out.println(s);System.out.println(the substring from index to 1 to index 3 is+s.substring(1,3);System.out.println(the substring from index to 3 to index 3 is+s.substring(3,3);System.out.println(the substring from index to 2 to index 3 is+s.substring(2,3);System.out.println
7、(the substring from index to 0 to index 3 is+s.substring(0,3);System.out.println(the substring from index to 3 to end is+s.substring(3);,实例5-5 改变字符串大小写,class Exam5_5 public static void main(String args)String s=HeLLo;System.out.println(the upper case is+s.toUpperCase();System.out.println(the lower c
8、ase is+s.toLowerCase();,实例5-6 字符串连接示例,class Exam5_6 public static void main(String args)String s1=he;String s2=llo;System.out.println(s1 concat s2 is+s1+s2);System.out.println(si concat s2 is+s1.concat(s2);,实例5-7 字符串比较示例,class Exam5_7public static void main(String args)String s1=aaaa;String s2=aaaa;
9、String s3=AAAA;String s4=bcd;if(s1.equals(s2)System.out.println(s1=s2);else System.out.println(s1!=s2);if(s1.equalsIgnoreCase(s3)System.out.println(s1=s3 when ignoring case);else System.out.println(s1!=s3 when ignoring case);if(pareTo(s4)s4);,实例5-8 字符串查找替换示例,classExam5_8 public static void main(Stri
10、ng args)String s=hello;System.out.println(s);System.out.println(the first index of l is+s.indexOf(l);System.out.println(the last index of l is+s.lastIndexOf(l);System.out.println(the string after replace is+s.replace(l,L);System.out.println(the string after trim is+s.trim();,实例5-9 字符串转换为基本数据类型示例,cla
11、ss Exam5_9 public static void main(String args)String s=1;System.out.println(s);System.out.println(s+3=+(Integer.parseInt(s)+3);,实例5-10 基本数据类型转换为字符串示例,class Exam5_10 public static void main(String args)int i=3;String s=String.valueOf(i);String s1=Integer.toString(i);String s2=i+;System.out.println(s
12、);System.out.println(s1);System.out.println(s2);,5.3 StringBuffer类字符串,String类是java中最常用的类之一,但它的实例是不可改变的,无论何时,只要一个字符串被改变了,就必须隐含或者显式的创建一个新的String对象来完成它。Java为需要改变的字符串对象提供了一个独立的StringBuffer类。StringBuffer与String类相似,它具有String类的很多功能,甚至更丰富。但是StringBuffer对象可以方便地在缓冲区内被修改,如增加、替换字符或子串,故特别适合于处理可变字符串。当完成了缓冲字符串数据操作
13、后,可以通过调用其方法StringBuffer.toString()或String构造器把它们有效地转换回标准字符串格式。,5.3.1 StringBuffer类字符串的定义,StringBuffer类提供了四个构造方法,可以使用这四个构造方法创建StringBuffer类的对象。这四个构造方法如下所示:StringBuffer()构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。StringBuffer(CharSequence seq)public java.lang.StringBuilder(CharSequence seq)构造一个字符串缓冲区,它包含与指定的 Char
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 字符串 正则 表达式
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-6509747.html