实验一 Java常用工具类编程
§1.1实验指导 1、String类使用
String 类表示字符串。 在 Java 程序中所有的字符串常量,如 \,都被实现为这个类的实例。 1)、构造函数
String s1=\
String s2=new String(\ 2)、比较函数
①==比较引用是否相同
if(s1==s2) //返回false ②equals():比较串内容是否相同 if(s1.equals(s2))//返回true ③compareTo():比较内容,返回数字 s1.compareTo(s2)
如果s1>s2 则返回>0 如果s1=s2 则返回0 如果s1
3)、取子串substring(),取字符charAt(index) String s1=\ s1.substring(start,end);
s1.substring(0,3);//start到end-1 String s2=s1.substring(6,10); 4)、分割split();
String s1=\ String[] str=s1.split(\
String s2=\
String s2=\ String[] str=s2.split(\\\\\.| \ for(int i=0;i
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(\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 = 2009 int thisMonth = myDate.getMonth() + 1;//thisMonth = 10 int thisDate = myDate.getDate();//thisDate = 30
3)按本地时区输出当前日期
Date myDate = new Date();
out.println(myDate.toLocaleString());
输出结果为: 2003-5-30
4)按照指定格式打印日期
import \
import \Date dNow = new Date();
SimpleDateFormat formatter =
new SimpleDateFormat(\System.out.println(\
输出的结果为:
It is 星期五 2003.05.30 at 11:30:46 上午 CST
5)将字符串转换为日期
import=\import=\
String input = \
SimpleDateFormat formatter = new SimpleDateFormat(\Date t = null; try{
t = formatter.parse(input); System.out.println(t); }catch(ParseException e){
System.out.println(\ }
输出结果为:
Fri Nov 11 00:00:00 CST 1222
6) 计算日期之间的间隔 getTime()函数返回日期与1900-01-01 00:00:00相差的毫秒数
import=\
import=\String input = \
SimpleDateFormat formatter = new SimpleDateFormat(\Date d1 = null;
Date d2 = new Date();long diff = d2.getTime() - d1.getTime(); out.println(\
输出结果为:
Difference is 29 days.
7) 日期的加减运算
方法:用Calendar类的add()方法
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat(\yyyy.MM.dd 'at' hh:mm:ss a zzz\
out.println(\now.add(Calendar.DAY_OF_YEAR,-(365*2));
out.println(\
out.println(\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=\import=\
DateFormat df = new SimpleDateFormat(\Date d1 = df.parse(\Date d2 = df.parse(\String relation = null;
if(d1.equals(d2))
relation = \else if(d1.before(d2)) relation = \else
relation = \
System.out.println(d1 +\
输出结果为:
Sat Jan 01 00:00:00 CST 2000 is after Fri Dec 31 00:00:00 CST 1999
§1.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;
相关推荐: