您好,欢迎来到步遥情感网。
搜索
您的当前位置:首页String类的使用

String类的使用

来源:步遥情感网
武汉工业学院上机实验报告

一、实验项目名称:字符串和I\\O 二、【实验目的】:

1. 掌握Java中的类的定义和应用

2. 掌握使用字符串String类处理字符串的方法 3. 掌握使用字符串StringBuffer类处理字符串的方法 4. 掌握命令行参数使用方法

三、【实验软件环境】:jdk6.0 四、【实验原理】或 【实验任务】:

1.利用下面的关键代码编写一个完整的程序,理解String类的使用。

String s=new String(\"This is an demo of the String method.\"); //String s=\"This is an demo of the String method.\"; System.out.println(\"Length: \"+s.length());

System.out.println(\"SubString: \"+s.substring(11,15));

2.利用下面的关键代码编写一个完整的程序,理解StringBuffer类的使用。

StringBuffer sb=new StringBuffer(\"Hello World!\"); sb.append(\" Hello Java!\"); sb.insert(12,\" And\"); System.out.println(sb);

System.out.println(sb.charAt(0));

sb.setCharAt(0,''h'');

System.out.println(sb.charAt(0)); System.out.println(sb);

五、【上机步骤】: (一):

程序代码:

public class StringDemo {

public static void main(String args[]){

String s1=\"This is an demo of the String method.\"; String s2=new String(\"This is an demo of the String method.\");

System.out.println(\"Length: \"+s1.length()); System.out.println(\"SubString: \"+s2.substring(11,15)); } }

运行结果: Length: 37 SubString: demo

(二):

程序代码:

public class StingBuffer {

public static void main(String args[]){

StringBuffer sb=new StringBuffer(\"Hello World!\"); sb.append(\" Hello Java!\"); sb.insert(12,\" And\"); System.out.println(sb);

System.out.println(sb.charAt(0)); sb.setCharAt(0,'h');

System.out.println(sb.charAt(0)); System.out.println(sb); } }

运行结果:

Hello World! And Hello Java! H h

hello World! And Hello Java!

【实验总结】:

1. 了解了各种变量的声明、定义;尤其是静态变量的声明和定义; 2. 区分了全局变量和局部变量的作用范围;

3. 掌握了一些 Java 程序的基本数据类型、运算符与表达;

4. 理解 Java 程序的语法结构(顺序结构、选择结构和循环结构)的程序设计方法;

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- obuygou.com 版权所有 赣ICP备2024042798号-5

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务