资源预览内容
第1页 / 共21页
第2页 / 共21页
第3页 / 共21页
第4页 / 共21页
第5页 / 共21页
第6页 / 共21页
第7页 / 共21页
第8页 / 共21页
第9页 / 共21页
第10页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
Java面向对象程序设计,-数组,2,本节内容,字符串操作类 java.lang.String类 构造方法 java.lang.StringBuffer类 java.util.StringTokenizer类 不同的应用场合,3,String类的构造方法,生成空字符串 public String() 从byte数组生成(0127) public String(bytebytes) public String(bytebytes, intoffset, intlength) public String(byte bytes, Charset charset) public String(byte bytes, intoffset, intlength, Charset charset) public String(bytebytes, StringcharsetName) ascii,utf-8,iso-8859-1,utf-16 public String(byte bytes, intoffset, intlength, StringcharsetName),String类的构造方法,从char数组生成 public String(charvalue) public String(charvalue, intoffset, intlength) 从int数组生成 public String(intcodePoints, intoffset, intcode) 1114111 从字符串生成 public String(Stringoriginal) public String(StringBuffer buffer) public String(StringBuilder builder),5,String类的构造方法,java.lang.String类字符串/字符序列 构造方法的使用,String a=abc“; String b=new String(); b=abc; String c=new String(a); String d=new String(abc); byte bytes=97,98,99; String e=new String(bytes); String f=new String(bytes,0,3); char value=a,b,c; String g=new String(value); int codePoint=97,98,99; String h=new String(codePoint,0,3);,a=abc; b=abc; c=abc; d=abc; e=abc; f=abc; g=abc; h=abc;,6,String类的构造方法,java.lang.String类字符串/字符序列 构造方法的使用,class Demo3 public static void main (String args) try byte bytes=97,98,99; String a=new String(bytes,ascii); System.out.println(a=+a); String b=new String(bytes,utf-8); System.out.println(b=+b); catch(Exception e) System.out.println(e); ,7,String类的比较方法,public boolean equals(ObjectanObject) 判断是否是同一个对象,是ture;然后anObject是否为一个字符串对象,否false;是判断是否内容相同 public boolean equalsIgnoreCase(StringanotherString) 判断逻辑与equals相同,仅仅在判断内容上不考虑大小写 public int compareTo(Objecto) 若o不是字符串对象,则抛错;若是则调用下面的函数 public int compareTo(StringanotherString) 判断两个字符串的内容是否相同,是0;否不等于0的整数 public int compareToIgnoreCase(Stringstr) 基本功能同上,仅仅在判断时不考虑大小写,class Demo4 public static void main (String args) String a=abc; String b=abc; String c=ABC; System.out.println(a.equals(b)=+a.equals(b); System.out.println(a.equals(c)=+a.equals(c); System.out.println(a.equalsIgnoreCase(c)=+a.equals(c); System.out.println(pareTo(b)=+pareTo(b); System.out.println(pareTo(c)=+pareTo(c); System.out.println(pareToIgnoreCase(c)=+pareTo(c); ,运行结果: a.equals(b)=true; a.equals(c)=false; a.equalsIgnoreCase(c)=true pareTo(b)=0 pareTo(c)=32 pareToIgnoreCase(c)=0,8,String类的取字符或子串方法,获取长度 public int length() 字符串的长度,即包含多少个字符 获取特定子串(substring)和字符 public String substring(intbeginIndex) public String substring(intbeginIndex, intendIndex) beginIndex: 起始索引位置(包含) endIndex: 结束索引位置(不包含) public char charAt(intindex),String s1 = java语言; String s2 = JavA语言; System.out.println(s1.length(); System.out.println(s2.length(); System.out.println(s1.substring(0, 4); System.out.println(s1.substring(4); System.out.println(s2.substring(0, 4); System.out.println(s2.substring(4); System.out.println(s1.charAt(0);,运行结果: 6 6 java 语言 JavA 语言 j,9,字符串操作类,java.lang.String类字符串/字符序列 字符串前缀(prefix)/后缀(suffix)的判断 public boolean startsWith(Stringprefix) 判断字符串是否以一特定的字符串开头 public boolean startsWith(Stringprefix, inttoffset) public boolean endsWith(Stringsuffix) 判断字符串是否以一特定的字符串结束,class Demo5 public static void main (String args) String a=abc; System.out.println(a.startsWith(ab)=+a.startsWith(ab); System.out.println(a.startsWith(bc“,1)=+a.startsWith(bc,1); System.out.println(a.endsWith(e)=+a.endsWith(c); ,a.startsWith(“ab”)=true; a.startsWith(“b”,1)=true; a.endsWith(“e”)=true;,10,String类查询字符/字符串,public int indexOf(intch) 该字符在字符串中第一次出现位置的索引值;否则返回-1 public int indexOf(intch, intfromIndex) public int indexOf(Stringstr) public int indexOf(Stringstr, intfromIndex) public int lastIndexOf(intch) public int lastIndexOf(intch, intfromIndex) public int lastIndexOf(Stringstr) public int lastIndexOf(Stringstr, intfromIndex),11,字符串操作类,java.lang.String类字符串/字符序列 方法举例,String s = “java语言”; System.out.println(s.indexOf(a); System.out.println(s.indexOf(a, 2); System.out.println(s.indexOf(“a”); System.out.println(s.indexOf(“语言”); System.out.println(s.lastIndexOf(a); System.out.println(s.lastIndexOf(v, 1); System.out.println(s.lastIndexOf(“语言”); System.out.println(s.lastIndexOf(“v”, 2);,运行结果: 1 3 1 4 3 -1 4 2,12,字符串操作类,java.lang.String类字符串/字符序列 字符串转变为数组 public byte getBytes() 将字符串转变为一个字节数组 public byte getBytes(StringcharsetName) throws UnsupportedEncodingException 按特定的字符编码格式将字符串转变为一个字节数组 public char toCharArray() 将字符串转变为一个字符数组,13,字符串操作类,java.lang.String类字符串/字符序列 方法举例,String s = java语言; char c = s.toCharArray(); System.out.println(c.length); byte b = s.getBytes(); System.out.println(b.length); b = s.getBytes(ISO8859-1); System.out.println(b.length);,运行结果: 6 8 6,中文Windows操作系统: 默认字符集 GB2312 其他系统: 默认字符集 ISO-8859-1,14,String类的其他方法,public String concat(Stringstr) 连接字符串 cares.concat(s) returns caress to.concat(get).concat(her) returns together public String replace(charoldChar, charnewChar) 在字符串中进行字符替换 mesquite in your cellar.replace(e, o) returns mosquito in your collar” JonL.r
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号