- 380.00 KB
- 2022-04-29 14:28:06 发布
- 1、本文档共5页,可阅读全部内容。
- 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
- 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
- 文档侵权举报电话: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
您可能关注的文档
- 煤的综合利用――苯01(课件PPT).ppt
- 二年级1班新生家长会课件PPT.ppt
- 二元一次方程组说课课件PPT.ppt
- 人教版五年级下美术《写意蔬果》课件PPT.pptx
- 让瘪的乒乓球鼓起来课件PPT下载_冀教版四年级科学下册课件.ppt
- 苏教版三年级下册数学《认识年、月、日》课件PPT.ppt
- 小学美术课件PPT《鱼儿欣赏》.ppt
- 苏教版国标本语文五年级上册第三单元复习课件PPT.docx
- 海带中碘元素的分离及检验(课件PPT).ppt
- 汽车精品销售培训课件PPT共.ppt
- 乳腺增生课件PPT.pdf
- 癫痫所致的精神障碍课件PPT.ppt
- 江苏省扬州市2017届高考化学一轮复习物质结构与性质4(17份打包)6(课件PPT).ppt
- 人教版初中语文八年级下册《关雎》教学课件PPT模板下载.pptx
- 人教版九年级下册数学《相似三角形》教学课件PPT模板下载.pptx
- 人教版初中语文七年级上册《散步》教学课件PPT模板下载.pptx
- 新概念英语第二册第57课课件PPT.ppt
- 英语选修7北师大版Unit19课件PPT:L1--14张.ppt