资源预览内容
第1页 / 共21页
第2页 / 共21页
第3页 / 共21页
第4页 / 共21页
第5页 / 共21页
第6页 / 共21页
第7页 / 共21页
第8页 / 共21页
第9页 / 共21页
第10页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
实验一 Java常用工具类编程1.1实验指导1、String类使用String 类表示字符串。 在 Java 程序中所有的字符串常量,如 abc,都被实现为这个类的实例。 1)、构造函数 String s1=java; String s2=new String(java); 2)、比较函数 =比较引用是否相同 if(s1=s2) /返回falseequals():比较串内容是否相同 if(s1.equals(s2)/返回true compareTo():比较内容,返回数字 s1.compareTo(s2) 如果s1s2 则返回0 如果s1=s2 则返回0 如果s1s2 则返回0 3)、取子串substring(),取字符charAt(index) String s1=hello java; s1.substring(start,end); s1.substring(0,3);/start到end-1 String s2=s1.substring(6,10); 4)、分割split(); String s1=c+,java,vb.net; String str=s1.split(,); String s2=c+.java.vb; String s2=c+.java.vb dotnet; String str=s2.split(.| ); for(int i=0;istr.length;i+) System.out.println(stri);2、StringBuffer类StringBuffer public StringBuffer()构造一个不包含字符的字符串缓冲区,其初始的容量设为 16 个字符。 StringBuffer public StringBuffer(int length):构造一个不包含字符的字符串缓冲区,其初始的容量由参数 length 设置。 StringBuffer public StringBuffer(String str):构造一个字符串缓冲区,来表示和字符串参数相同的字符序列。 length public int length():返回字符串缓冲区的长度 (字符数)。 capacity public int capacity():返回字符串缓冲区的当前容量。 该容量表示可用于插入新的字符的存储空间;超出该容量时会发生新的容量分配。 ensureCapacity public synchronized void ensureCapacity(int minimumCapacity):保证缓冲区的容量至少等于指定的最小数。 如果字符串缓冲区的当前容量少于该参数,则分配一个新的更大的内部缓冲区。 新容量将取如下参数中较大的一个: setLength public synchronized void setLength(int newLength):设置该字符串缓冲区的长度。 如果参数 newLength 小于该字符串缓冲区的当前长度。 该字符串缓冲区将被截断来包含恰好等于由参数 newLength 给出的字符数。 append public synchronized StringBuffer append(Object obj):把 Object 型参数的字符串表示添加到该字符串缓冲区。 StringBuffer x = new StringBuffer().append(a).append(4).append(c) .toString();insert public synchronized StringBuffer insert(int offset, Object obj):把 Object 型参数的字符串表示插入到字符串缓冲区。 reverse public synchronized StringBuffer reverse():该字符串缓冲区的字符序列被其反向字符序列所替换。 toString public String toString():转换为一个表示该字符串缓冲区数据的字符串。 分配一个新的 String 对象,并且用字符串缓冲区所表示的字符序列进行初始化。 于是此 String 被返回。 随后缓冲区发生的变化不再影响该 String 的内容。 3、日期类示例1)获取服务器端当前日期: import java.util.Date; Date myDate = new Date();2) 获取当前年、月、日:Date myDate = new Date();int thisYear = myDate.getYear() + 1900;/thisYear = 2009int thisMonth = myDate.getMonth() + 1;/thisMonth = 10int thisDate = myDate.getDate();/thisDate = 303)按本地时区输出当前日期Date myDate = new Date();out.println(myDate.toLocaleString();输出结果为:2003-5-30 4)按照指定格式打印日期import java.util.Date;import java.text.DateFormat;Date dNow = new Date();SimpleDateFormat formatter =new SimpleDateFormat(E yyyy.MM.dd at hh:mm:ss a zzz);System.out.println(It is + formatter.format(dNow);输出的结果为:It is 星期五 2003.05.30 at 11:30:46 上午 CST 5)将字符串转换为日期import=java.util.Dateimport=java.text.DateFormatString input = 1222-11-11;SimpleDateFormat formatter = new SimpleDateFormat(yyyy-MM-dd);Date t = null;try t = formatter.parse(input);System.out.println(t); catch(ParseException e) System.out.println(unparseable using + formatter); 输出结果为:Fri Nov 11 00:00:00 CST 1222 6) 计算日期之间的间隔 getTime()函数返回日期与1900-01-01 00:00:00相差的毫秒数import=java.util.Dateimport=java.text.DateFormatString input = 2003-05-01;SimpleDateFormat formatter = new SimpleDateFormat(yyyy-MM-dd);Date d1 = null;Date d2 = new Date();long diff = d2.getTime() - d1.getTime();out.println(Difference is + (diff/(1000*60*60*24) + days.);输出结果为:Difference is 29 days. 7) 日期的加减运算方法:用Calendar类的add()方法Calendar now = Calendar.getInstance();SimpleDateFormat formatter = new SimpleDateFormat(E yyyy.MM.dd at hh:mm:ss a zzz);out.println(It is now + formatter.format(now.getTime();now.add(Calendar.DAY_OF_YEAR,-(365*2);out.println();out.println(Two years ago was + formatter.format(now.getTime();输出结果为:It is now 星期五 2003.05.30 at 01:45:32 下午 CST Two years ago was 星期三 2001.05.30 at 01:45:32 下午 CST 8) 比较日期方法:用equals()、before()、after()方法import=java.util.* import=java.text.* DateFormat df = new SimpleDateFormat(yyy-MM-dd);Date d1 = df.parse(2000-01-01);Date d2 = df.parse(1999-12-31);String relation = null;if(d1.equals(d2) relation = the same date as;else if(d1.before(d2) relation = before;else relation = after;System.out.println(d1 + is + relation + + d2);输出结果为:Sat Jan 01 00:00:00 CST 2000 is after Fri Dec 31 00:00:00 CST 19991.2实验题目1、 使用类String类的分割split 将字符串 “Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from BruceEckel” 单词提取输出。单词以空格或,分割。package job1;public class Split public static void main(String args)String s1=Solutions to selected exercises +can be found in the electronic document +The Thinking in Java Annotated Solution Guide, +available for a small fee from BruceEckel;String s2=s1.split( |,);for(int i=0;is2.length
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号