云南大学软件学院实验报告姓名:王定欢学号:20141120188班级:日期:2016.10.13成绩:JAVA实验四一、实验目的:FundamentalProgrammingStructuresinJava:Strings二、实验要求:1.WriteaprogramcalledString1.javaanddothefollowing:CreateaStringobjectthatacceptsinputfromtheuser.ReferencingtheJDKdocs,execute2differentmethodsontheString.Printtheresultsofthemethods.2.WriteaprogramcalledStringBuffer1.javathatdoesthefollowing:CreateaStringBufferobject.ReferencingtheJDKdocs,execute2differentmethodsontheStringBuffer.Printtheresultsofthemethods.3.WriteaprogramcalledStringTokenizer1.javathatacceptsastring,looksforcommaswithinthestring,andbreaksthestringupintothepartsseparatedbythecomma.Forexample,thestringKunming,Yunnan,Chinawouldreturnthreestrings:String1=KunmingString2=YunnanString3=China三、实验内容:1、①代码:packagelab4;importjava.util.Scanner;publicclassString1{publicstaticvoidmain(String[]args){System.out.println(pleaseinputastring);Scannerin=newScanner(System.in);Stringinput=in.nextLine();System.out.println(theinputstringis:+input);input.toUpperCase();//将字符串中的小写字母全部转化为大写字母System.out.println(theuppercasestringis:+input.toUpperCase());System.out.println(thestringlengthis:+input.length());}}②实验结果:2、①代码:packagelab4;publicclassStringBuffer1{publicstaticvoidmain(String[]args){StringBuilderbuilder=newStringBuilder();builder.append(hello).append(world);StringcompleteString=builder.toString();//构建字符串方法System.out.println(thecompletestringis:+completeString);StringBuilderinsertString=builder.insert(10,Ilovejava);//将字符插入到字符串中System.out.println(afterinsertstringis:+insertString);}}②、实验结果:3、①代码:packagelab4;importjava.util.Scanner;importjava.util.StringTokenizer;publicclassStringTokenizer1{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);Stringinputtest=newString();System.out.println(pleaseinputastringwithcommas:);inputtest=in.nextLine();StringTokenizerstr=newStringTokenizer(inputtest,,);//用StringTokenizer方法将字符串分段intj=1;while(str.hasMoreElements()){System.out.println(String+j+=+str.nextToken());j++;}StringBuilderrs=newStringBuilder();//倒序输出for(inti=inputtest.length()-1;i-1;i--){rs.append(inputtest.charAt(i));}System.out.println(逆序为:+rs);}}②实验结果:4、附加题:①代码:packagelab4;importjava.util.StringTokenizer;publicclasslab4{publicstaticvoidmain(String[]args){Stringname=newString(Mynameislilei);System.out.println(thelengthofstringis:+name.length());charfirstChar=name.charAt(0);//第一个位置System.out.println(thefirstcharis:+firstChar);System.out.println(thelastcharis:+name.substring(name.length()-1));//最后一个位置StringTokenizerstr=newStringTokenizer(name,);System.out.println(thefirstworldis:+str.nextToken());String[]str1=name.split();//找出lilei的位置intlength=str1.length;System.out.println(theplaceofLiLeiis:+length);}}②、实验结果四、实验总结:本次实验通过运用字符串的部分函数,进一步的熟练了对字符串的使用。指导教师签名: