字符串实验一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); //跳转按钮添加监听事

半月湾($59.99/年),升级带宽至200M起步 三网CN2 GIA线路

在前面的文章中就有介绍到半月湾Half Moon Bay Cloud服务商有提供洛杉矶DC5数据中心云服务器,这个堪比我们可能熟悉的某服务商,如果我们有用过的话会发现这个服务商的价格比较贵,而且一直缺货。这里,于是半月湾服务商看到机会来了,于是有新增同机房的CN2 GIA优化线路。在之前的文章中介绍到Half Moon Bay Cloud DC5机房且进行过测评。这次的变化是从原来基础的年付49....

青云互联:香港安畅CN2弹性云限时首月五折,15元/月起,可选Windows/可自定义配置

青云互联怎么样?青云互联是一家成立于2020年的主机服务商,致力于为用户提供高性价比稳定快速的主机托管服务,目前提供有美国免费主机、香港主机、韩国服务器、香港服务器、美国云服务器,香港安畅cn2弹性云限时首月五折,15元/月起;可选Windows/可自定义配置,让您的网站高速、稳定运行。点击进入:青云互联官方网站地址青云互联优惠码:八折优惠码:ltY8sHMh (续费同价)青云互联香港云服务器活动...

RepriseHosting:$27.97/月-L5640,16G内存,1TB硬盘,10TB月流量,西雅图机房

RepriseHosting是成立于2012年的国外主机商,提供独立服务器租用和VPS主机等产品,数据中心在美国西雅图和拉斯维加斯机房。商家提供的独立服务器以较低的价格为主,目前针对西雅图机房部分独立服务器提供的优惠仍然有效,除了价格折扣外,还免费升级内存和带宽,商家支持使用支付宝或者PayPal、信用卡等付款方式。配置一 $27.97/月CPU:Intel Xeon L5640内存:16GB(原...

java编程工具为你推荐
可以发外链的论坛可以发外链的论坛有那些?深圳公交车路线深圳公交车路线查询金山杀毒怎么样金山杀毒好吗ghostxp3ghost xp sp3 和 windows xp3有啥区别网站运营刚创业的网站运营怎么做?照片转手绘照片弄成手绘一样的那个软件到底叫什么,能不能告诉啊?照片转手绘怎么把图片P成手绘唱吧电脑版官方下载唱吧有电脑版吗宕机宕机是什么意思云挂机云软件挂机赚钱是骗子
美国虚拟主机空间 阿里云邮箱登陆首页 Dedicated 美国便宜货网站 softbank官网 免费网站监控 web服务器架设 七夕促销 河南移动网 太原网通测速平台 空间技术网 网通服务器托管 鲁诺 免费mysql数据库 lick 石家庄服务器托管 lamp架构 双线空间 ledlamp 1美元 更多