字符串实验一Java常用工具类编程

java编程工具  时间:2021-02-26  阅读:()

Java EE课程实 验

题 目: 实验一Java常 用工具类编 程学 号 20093 272姓 名 顾双凯

院 系 计算机与信 息工程学院专业年级 软件工程2 009级

2011 年 9 月 20 日

实验一J ava常 用工具类编 程

§ 1.1实验指导

1、 Strin g类使用

String类表示字符串。 在Java程序中所有的字符串常量如"abc"都被实现为这个类的实例。

1) 、构造函数

String s1="java";

String s2=new String("java") ;

2) 、 比较函数

①==比较引用是否相同if(s1==s2) //返回fal se

②equals() :比较串内容是否相同if(s1. equals(s2) )//返回tru e

③compareTo() :比较内容,返回数字s1.compareTo(s2)

如果s1>s2 则返回>0

如果s1=s2 则返回0

如果s1<s2 则返回<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; i<str. length; i++)

System.out.println(str[i]) ;

2、 Strin gBuff er类

StringBuffe rpubl ic StringBuffer()

构造一个不包含字符的字符串缓冲区其初始的容量设为16个字符。

StringBuffe rpubl ic StringBuffer(int length)构造一个不包含字符的字符串缓冲区其初始的容量由参数l e n g th设置。

StringBuffe rpubl ic StringBuffer(String str)构造一个字符串缓冲区来表示和字符串参数相同的字符序列。lengthpubl icint length()返回字符串缓冲区的长度(字符数)。capacitypubl ic intcapacity()返回字符串缓冲区的当前容量。 该容量表示可用于插入新的字符的存储空间超出该容量时会发生新的容量分配。ensureCapacitypublic synchronized void ensureCapacity(int minimumCapacity) 保证缓冲区的容量至少等于指定的最小数。 如果字符串缓冲区的当前容量少于该参数 则分配一个新的更大的内部缓冲区。 新容量将取如下参数中较大的一个setLengthpublic synchronized void setLength(int newLength) 设置该字符串缓冲区的长度。 如果参数newLength小于该字符串缓冲区的当前长度。 该字符串缓冲区将被截断来包含恰好等于由参数newLength给出的字符数。appendpublic synchronized StringBuffer append(Object obj) 把Object型参数的字符串表示添加到该字符串缓冲区。

StringBuffer x=new StringBuffer().append("a").append(4).append("c") .toString();insertpublic synchronized StringBuffer insert(int offset, Object obj) 把Object型参数的字符串表示插入到字符串缓冲区。reversepublic synchronized StringBuffer reverse() 该字符串缓冲区的字符序列被其反向字符序列所替换。toStringpublic 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 = 30

3)按本地时区输出当前日期

Date myDate = new Date() ;

out.println(myDate. toLocaleString() ) ;

输出结果为

2003-5-30

4)按照指定格式打印日期impor t "java.util.Date";impor t "java. text.DateFormat";

Date dNow = new Date() ;

SimpleDateFormat formatter =new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz") ;

Syste m.out.println("It is " + formatter.format(dNow) ) ;

输出的结果为

It is星期五2003.05.30 at 1 1 :30:46上午CST

5)将字符串转换为日期import="java.util.Date"import="java. text.DateFormat"

String input = "1222-11-11";

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd") ;Date t = null;try{t = formatter.parse(input) ;

Syste m.out.println(t) ;

}catch(ParseException e) {

Syste m.out.println("unparseable using" + formatter) ;

}

输出结果为

Fri Nov 1 1 00:00:00 CST 1222

6)计算日期之间的间隔getTi me()函数返回日 期与190 0-01-0100:00:00相差的毫秒数

Import java.util.Date;import java. text.DateFormat;

String 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("<br>") ;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()方法impor t java.util.*;impor t 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";elserelation = "after";

System.out.println(d1 +" is " + relation + ' ' + d2) ;

输出结果为

Sat Jan 01 00:00:00 CST 2000 is afterFri Dec 3100:00:00 CST 1999

§ 1.2实验题目

1、使用类St ring类 的分割sp l it将字符串 “Solutions to selec tedexercises can befound in the elect ronic document The ThinkinginJavaAnnot atedSolut ion Guide , available fora small fee from

Bru ce Eckel”单词提取输 出。单词以空格或,分割。package SY1;public class Demo1 {

/**

* @param args

*/public static void main(String[ ] args) {

// TODO Auto-generated method stub

String s="Solutions to selected exercises can be found in theelectronic document The Thinking in Java Annotated Solution Guide,available for a small fee from BruceEckel";

String[ ] sp=s.split(" | , ") ;for(int i=0;i<sp.length;i++)

System.out.println(sp[i] ) ;

}

}

2、设计一个类 Stude nt,类的属性有 姓名 学号 出生日期性别所在系等。并生成学生 类对象数组 。按照学生的 姓名将学生 排序输出。使用Str ing类的 compa reTo方 法。

3、设计一个程 序计算20 10-05-01 日与系 统当前日期 相差的天数 。

package SY1;import java.util.Date;import java.text.*;public class Demo4 {public static void main(String[ ] args) {

String input="2010-05-01";

SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd") ;

Date d1=null;try{d1 = formatter.parse(input) ;

}catch(ParseException e) {

System.out.println("unparseable using" + formatter) ;

}

Date d2 = new Date( ) ;long diff = d2.getTime( ) - d1.getTime( ) ;

System.out.println("Difference is " + (diff/ (1000*60*60*24) ) + "days. ") ;

}

}

4、使用日历类 等相关方法 按截图做出 一个日历参照书本示 例,研究其中代码回顾与复 习利用Ja va Swing 编程。

package SY1;import java.awt.*;import java.awt.event.*;importjavax.swing.*;importjava.uti l.*;public class CalenderTrain extends JFrame implements ActionListener{JComb oBoxMonth =newJComb oBox(); //月份下拉列表框JComb oBoxYear=newJComb oBox(); //年份下拉列表框JLabe lYear_ l=newJLabe l("年:");

JLabe l Month _l=newJLabe l("月:");

Date now_d ate=new Date(); //获取今天的 日期

JButt on[]butto n_day =new JButt on[49]; //定义一个数组用来存放 日期

JButt on butto n_ok=newJButt on("跳转"); //现实选择日 期JButt on butto n_tod ay=new JButt on("现在日期"); //显示今天日 期按钮int now_y ear=now_d ate.getYe ar()+1900; //获取年份值int now_m onth=now_d ate.getMo nth(); //获取月份值(当前月份-1)boolean bool=false;

Strin gyear_ int=nul l; //存放年份intmonth _int; //存放月份

JPane l pane_ ym=new JPane l(); //放置下拉列表框和控制 按钮面板

JPane lpane_ day=newJPane l(); //放置日期面板

JPane lpane_ paren t=newJPane l(); //放置以上两 个面板publicCalenderTrain() {super ("Calen der!"); //设定面板得titlefor(int i=now_year-20; i<=now_year+20; i++){

Year.addItem(i+"");

}for(int i=1; i<13; i++) {

Month.addItem(i+"");

}

Year.setSe lecte dInde x(10); //设定年份下拉列表为当 前年份pane_ ym.add(Year_ l); //添加年份标签pane_ ym.add(Year); //添加年份下拉列表框

Month .setSe lecte dInde x(now_m onth); //设定月份下拉列表为当 前月份pane_ ym.add(Month _l); //添加月份标签pane_ ym.add(Month ); //添加月份下拉列表框pane_ ym.add(butto n_ok); //添加跳转按钮pane_ ym.add(butto n_tod ay); //添加“现在日期”按钮butto n_ok.addAc tionL isten er(this); //跳转按钮添加监听事

Megalayer促销:美国圣何塞CN2线路VPS月付48元起/香港VPS月付59元起/香港E3独服月付499元起

Megalayer是新晋崛起的国外服务器商,成立于2019年,一直都处于稳定发展的状态,机房目前有美国机房,香港机房,菲律宾机房。其中圣何塞包括CN2或者国际线路,Megalayer商家提供了一些VPS特价套餐,譬如15M带宽CN2线路主机最低每月48元起,基于KVM架构,支持windows或者Linux操作系统。。Megalayer技术团队行业经验丰富,分别来自于蓝汛、IBM等知名企业。Mega...

TabbyCloud周年庆&七夕节活动 美國INAP 香港CN2

TabbyCloud迎来一周岁的生日啦!在这一年里,感谢您包容我们的不足和缺点,在您的理解与建议下我们也在不断改变与成长。为庆祝TabbyCloud运营一周年和七夕节,TabbyCloud推出以下活动。TabbyCloud周年庆&七夕节活动官方网站:https://tabbycloud.com/香港CN2: https://tabbycloud.com/cart.php?gid=16购买链...

无视CC攻击CDN ,DDOS打不死高防CDN,免备案CDN,月付58元起

快快CDN主营业务为海外服务器无须备案,高防CDN,防劫持CDN,香港服务器,美国服务器,加速CDN,是一家综合性的主机服务商。美国高防服务器,1800DDOS防御,单机1800G DDOS防御,大陆直链 cn2线路,线路友好。快快CDN全球安全防护平台是一款集 DDOS 清洗、CC 指纹识别、WAF 防护为一体的外加全球加速的超强安全加速网络,为您的各类型业务保驾护航加速前进!价格都非常给力,需...

java编程工具为你推荐
主页改不了浏览器主页改不了 怎么办啊快速美白好方法脸部快速美白有什么好方法啊eset最新用户名密码eset smart security3.0.621.0最新用户名和密码怎么找微信如何建群微信如何建群ps抠图技巧ps的抠图技巧是什么保护气球抖音里面看的,这是什么游戏网络广告投放怎样在网络上进行广告的投放?电子商务网站模板网页制作模板blogcn怎样设置BLOGCN的访问密码去鼠标加速度win7怎么去鼠标加速度
virpus t牌 分销主机 z.com uk2 omnis evssl证书 腾讯实名认证中心 1g空间 彩虹云 空间首页登陆 中国电信测速网站 主机返佣 国内空间 博客域名 新疆服务器 学生机 美国主机侦探 cx域名 qq空间打开很慢 更多