云南大学软件学院Java实验四
云南大学软件学院 实 验 报 告 姓名: 王定欢 学号: 班级: 日期: 2016.10.13 成绩: JAVA实验四 一、 实验目的: Fundamental Programming Structures in Java: Strings 二、 实验要求: 1. Write a program called String1.java and do the following: Create a String object that accepts from the user. Referencing the JDK docs, cute 2 different s on the String. Print the results of the s. 2. Write a program called StringBuffer1.java that does the following: Create a StringBuffer object. Referencing the JDK docs, cute 2 different s on the StringBuffer. Print the results of the s. 3. Write a program called StringTokenizer1.java that accepts a string, looks for commas within the string, and breaks the string up into the parts separated by the comma. For example, the string “Kunming, Yunnan, China“ would return three strings: String1 = “Kunming“ String2 = “Yunnan“ String3 = “China“ 三、 实验内容: 1、①代码: package lab4; import java.util.Scanner; public class String1 { public static void main(String[] args){ System.out.println(“please a string“); Scanner in = new Scanner(System.in); String = in.nextLine(); System.out.println(“the string is:“+); .toUpperCase();//将字符串中的小写字母全部转化为大写字母 System.out.println(“the uppercase string is:“+.toUpperCase()); System.out.println(“the string length is:“+.length()); } } ② 实验结果: 2、①代码: package lab4; public class StringBuffer1 { public static void main(String[] args){ StringBuilder builder = new StringBuilder(); builder.append(“hello“).append(“world“); String completeString = builder.toString();//构建字符串方法 System.out.println(“the complete string is:“+completeString); StringBuilder insertString = builder.insert(10,“I love java“);//将字符插入到字符串中 System.out.println(“after insert string is:“+insertString); } } ②、实验结果: 3、①代码: package lab4; import java.util.Scanner; import java.util.StringTokenizer; public class StringTokenizer1 { public static void main(String[] args){ Scanner in = new Scanner(System.in); String test = new String(); System.out.println(“please a string with commas:“); test = in.nextLine(); StringTokenizer str = new StringTokenizer(test,“,“);//用StringTokenizer方法将字符串分段 int j=1; while(str.hasMoreElements()){ System.out.println(“String“+j+“=“+str.nextToken()); j++; } StringBuilder rs = new StringBuilder();//倒序输出 for(int i = test.length()-1;i>-1;i--){ rs.append(test.charAt(i)); } System.out.println(“逆序为:“+rs); } } ②实验结果: 4、附加题: ①代码:package lab4; import java.util.StringTokenizer; public class lab4 { public static void main(String[]args){ String name = new String(“My name is lilei“); System.out.println(“the length of string is:“+name.length()); char firstChar = name.charAt(0);//第一个位置 System.out.println(“the first char is:“+firstChar); System.out.printl