• 380.00 KB
  • 2022-04-29 14:28:06 发布

java语言程序设计基础篇(第八版)课件PPT第九章 机械工业出版报社 李娜译.ppt

  • 58页
  • 当前文档由用户上传发布,收益归属用户
  1. 1、本文档共5页,可阅读全部内容。
  2. 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
  3. 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
  4. 文档侵权举报电话:19940600175。
'Chapter9StringsandTextI/O1 MotivationsOftenyouencountertheproblemsthatinvolvestringprocessingandfileinputandoutput.Supposeyouneedtowriteaprogramtoreplacealloccurrencesofawordwithanewwordinafile.Howdoyousolvethisproblem?Thischapterintroducesstringsandtextfiles,whichwillenableyoutosolvethisproblem.2 ObjectivesTousetheStringclasstoprocessfixedstrings(§9.2).TousetheCharacterclasstoprocessasinglecharacter(§9.3).TousetheStringBuilder/StringBufferclasstoprocessflexiblestrings(§9.4).TodistinguishamongtheString,StringBuilder,andStringBufferclasses(§9.2-9.4).Tolearnhowtopassargumentstothemainmethodfromthecommandline(§9.5).TodiscoverfilepropertiesandtodeleteandrenamefilesusingtheFileclass(§9.6).TowritedatatoafileusingthePrintWriterclass(§9.7.1).ToreaddatafromafileusingtheScannerclass(§9.7.2).(GUI)Toopenfilesusingadialogbox(§9.8).3 TheStringClassConstructingaString:Stringmessage="WelcometoJava“;Stringmessage=newString("WelcometoJava“);Strings=newString();ObtainingStringlengthandRetrievingIndividualCharactersinastringStringConcatenation(concat)Substrings(substring(index),substring(start,end))Comparisons(equals,compareTo)StringConversionsFindingaCharacteroraSubstringinaStringConversionsbetweenStringsandArraysConvertingCharactersandNumericValuestoStrings4 ConstructingStringsStringnewString=newString(stringLiteral);Stringmessage=newString("WelcometoJava");Sincestringsareusedfrequently,Javaprovidesashorthandinitializerforcreatingastring:Stringmessage="WelcometoJava";5 StringsAreImmutableAStringobjectisimmutable;itscontentscannotbechanged.Doesthefollowingcodechangethecontentsofthestring?Strings="Java";s="HTML";6 TraceCodeStrings="Java";s="HTML";animation7 TraceCodeStrings="Java";s="HTML";animation8 InternedStringsSincestringsareimmutableandarefrequentlyused,toimproveefficiencyandsavememory,theJVMusesauniqueinstanceforstringliteralswiththesamecharactersequence.Suchaninstanceiscalledinterned.Forexample,thefollowingstatements:9 Examplesdisplays1==sisfalses1==s3istrueAnewobjectiscreatedifyouusethenewoperator.Ifyouusethestringinitializer,nonewobjectiscreatediftheinternedobjectisalreadycreated.10 TraceCodeanimation11 TraceCode12 TraceCode13 StringComparisons14 StringComparisonsequalsStrings1=newString("Welcome“);Strings2="welcome";if(s1.equals(s2)){//s1ands2havethesamecontents}if(s1==s2){//s1ands2havethesamereference}15 StringComparisons,cont.compareTo(Objectobject)Strings1=newString("Welcome“);Strings2="welcome";if(s1.compareTo(s2)>0){//s1isgreaterthans2}elseif(s1.compareTo(s2)==0){//s1ands2havethesamecontents}else//s1islessthans216 StringLength,Characters,andCombiningStrings17 FindingStringLengthFindingstringlengthusingthelength()method:message="Welcome";message.length()(returns7)18 RetrievingIndividualCharactersinaStringDonotusemessage[0]Usemessage.charAt(index)Indexstartsfrom019 StringConcatenationStrings3=s1.concat(s2);Strings3=s1+s2;s1+s2+s3+s4+s5sameas(((s1.concat(s2)).concat(s3)).concat(s4)).concat(s5);20 ExtractingSubstrings21 ExtractingSubstringsYoucanextractasinglecharacterfromastringusingthecharAtmethod.YoucanalsoextractasubstringfromastringusingthesubstringmethodintheStringclass.Strings1="WelcometoJava";Strings2=s1.substring(0,11)+"HTML";22 Converting,Replacing,andSplittingStrings23 Examples"Welcome".toLowerCase()returnsanewstring,welcome."Welcome".toUpperCase()returnsanewstring,WELCOME."Welcome".trim()returnsanewstring,Welcome."Welcome".replace("e","A")returnsanewstring,WAlcomA."Welcome".replaceFirst("e","AB")returnsanewstring,WABlcome."Welcome".replace("e","AB")returnsanewstring,WABlcomAB."Welcome".replace("el","AB")returnsanewstring,WABlcome.24 SplittingaStringString[]tokens="Java#HTML#Perl".split("#",0);for(inti=0;i