文档详情

Java语言程序设计(第二版)电子教案贾振华 第7章 字符串处理.ppt

发布:2017-08-17约3.61千字共14页下载文档
文本预览下载声明
字符串处理 第7章 目标 String类字符串的定义及其基本操作 StringBuffer类字符串的定义及其基本操作 7.1 字符串 字符串是字符的序列,它是组织字符的基本数据结构,从某种程度上来说有些类似于字符的数组。在Java中,字符串被当作对象来处理。程序中需要用到的字符串可以分为两大类,一类是创建之后不会再做修改和变动的字符串常量String类;另一类是创建之后允许再做更改和变化的字符串变量StringBuffer类。 7.1.1 字符串常量 使用“”定义字符串,使用‘’定义字符。 “abc”,”欢迎使用Java”都是字符串。 ‘a’,’b’,’c’都是字符。 Java会自动为字符串常量生成一个String类的对象,所以可以直接初始化String对象,如:String s = “Hello world!” 7.2.1 String类字符串 创建String的一个对象并进行初始化,需要调用类String的构造方法,主要有以下创造方法: ①String():创建一个空串 ②String(String value) ③String(char value[]) ④String(char[],int startIndex,int numChars) ⑤String(byte[],byte hibyte) ⑥String(byte[],byte hibyte,int startIdnex,int numChars) 7.2.2 String类的基本操作4-1 (1)public int length( ):返回字符串长度(注意汉字) (2)字符串比较: public int compareTo(String anotherString) public boolean equals(Object anObject) public boolean equalsIgnore(String anotherString) public boolean startsWith(String prefix) public boolean endsWith(String suffix) boolean regionMatches(int toffset,String other,int ooffset,int len) boolean regionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len) 7.2.2 String类的基本操作4-2 (3)字符串的检索和子串 public char charAt(int index) public void getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin) public int indexOf(int ch)和 public int lastIndexOf(int ch) public int indexOf(int ch,int fromIndex) public int lastIndexOf(int ch,int fromIndex) public int indexOf(String str) public int lastIndexOf(String str) public int indexOf(String str,int fromIndex) public int lastIndexOf(String str,int fromIndex) public String substring(int beginIndex) public String substring(int beginIndex,int endIndex) 7.2.2 String类的基本操作4-3 (4)字符串的修改 public String toLowerCase() public String toUpperCase() public String replace(char oldChar,char newChar) public String replaceFirst(String rx,String replacement) public String replaceAll(String rx,String replacement) String trim() public String concat(String str)
显示全部
相似文档