文档详情

第五篇 常用类.ppt

发布:2017-06-02约5.77千字共25页下载文档
文本预览下载声明
第五章 常用类 本章内容 数 组 字符串相关类( String、StringBuffer) 基本数据类型包装类 Math类 日期时间类 容器类 String 类 java.lang.String 类代表不可变的字符序列。 “xxxxx”为该类的一个对象。 String 类的常见构造方法: string(String original) 创建一个String对象为original 的拷贝。 String(char[] value) 用一个字符数组创建一个String对象 String(char[] value, int offset, int count) 用一个字符数组从offset项开始的count个字符序列创建一个String对象。 String 类举例 public class Test { public static void main(String[ ] args) { String s1=“hello”; String s2=“world”; String s3=“hello”; System.out.println(s1==s3); //true s1=new String (“hello”); s2=new String(“hello”); System.out.println(s1==s2); //false System.out.println(s1.equals(s2)); //true char c[ ]={‘s’,’u’,’n’,’ ‘,’j’,’a’,’v’,’a’}; String s4=new String(c); String s5=new String(c,4,4); System.out.println(s4); //sun java System.out.println(s5); //java String 类常用方法(1) public char charAt( int index) 返回字符串中第 index 个字符。 public int length( ) 返回字符串的长度 public int indexOf( String str) 返回字符串中出现 str 的第一个位置 public int indexOf( String str, int fromIndex) 返回字符串中从fromIndex开始出现str的第一个位置 public boolean equalsIgnoreCase( String another) 比较字符串与another 是否一样(忽略大小写) public String replace(char oldChar, char newChar) 在字符串中用newChar字符替换oldChar字符 String 类举例(2) String 类举例(3) String 类常用方法 (2) public boolean startsWith( String prefix) 判断字符串是否以prefix字符串开头 public boolean endsWith( String suffix) 判断字符串是否以suffix字符串结尾 public String toUpperCase( ) 返回一个字符串为该字符串的大写形式 public String toLowerCase( ) 返回一个字符串为该字符串的小写形式 public String substring( int beginIndex) 返回该字符串从beginIndex开始到结尾的子字符串 public String substring( int beginIndex, int endIndex) 返回该字符串从beginIndex开始到endIndex结尾的子字符串 public String trim( ) 返回该字符串去掉开头和结尾空格后的字符串 String 类常用方法 (3) 静态重载方法 public static String valueOf(…) 可以将基本类型数据转换为字符串;例如: public static String valueOf(double d) public static St
显示全部
相似文档