《Java编程导论》课程教学资源(PPT课件讲稿)Chapter 8 Strings and Text I/O

Chapter 8 Strings and Text 1/O Chapter 6 Arrays Chapter 7 Objects and Classes Chapter 8 Strings and Text 1/O GUI can be covered after $10.2, "Abstract Classes Chapter 9 Inheritance and Polymorphism CHapter 12 GUI Basics $10.2,Abstract Classes Chapter 13 Graphics §10.4, Interfaces Chapter 14 Event-Driven Pi mIng Chapter 11 Object-Oriented Design Exception and binary 1O can be covered after Chapter 9 Chapter 17 Exceptions and Assertions Chapter 18 Binary 1O Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 1 Chapter 8 Strings and Text I/O Chapter 7 Objects and Classes Chapter 8 Strings and Text I/O Chapter 9 Inheritance and Polymorphism Chapter 6 Arrays §10.2, “Abstract Classes” §10.4, “Interfaces” Chapter 13 Graphics Chapter 14 Event-Driven Programming Chapter 17 Exceptions and Assertions Chapter 18 Binary I/O Exception and binary I/O can be covered after Chapter 9 GUI can be covered after §10.2, “Abstract Classes” Chapter 12 GUI Basics Chapter 11 Object-Oriented Design

Objectives To use the String class to process fixed strings($8.2) To use the Character class to process a single character($8.3) To use the String Builder/string buffer class to process flexible strings(§84 o know the differences between the String, String Builder, and String Buffer classes(882-8.4) To learn how to pass strings to the main method from the command line(§8.5) (Optional) To use the regular expressions to represent patterns for matching, replacing, and splitting strings(88.6) To discover file properties, delete and rename files using the file class(§8.7) To write data to a file using the Print Writer class($8.8.1) To read data from a file using the Scanner class($8.8. 2) Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 2 Objectives To use the String class to process fixed strings (§8.2). To use the Character class to process a single character (§8.3). To use the StringBuilder/StringBuffer class to process flexible strings (§8.4). To know the differences between the String, StringBuilder, and StringBuffer classes (§8.2-8.4). To learn how to pass strings to the main method from the command line (§8.5). (Optional) To use the regular expressions to represent patterns for matching, replacing, and splitting strings (§8.6). To discover file properties, delete and rename files using the File class (§8.7). To write data to a file using the PrintWriter class (§8.8.1). To read data from a file using the Scanner class (§8.8.2)

The string Class Constructing a String String message =Welcome to Java String message new String("Welcome to Java) String s= new string()i Obtaining String length and Retrieving Individual characters in a string String Concatenation(concat Substrings(substring(index), substring(start, end)) Comparisons(equals, compareTo String Conversions Finding a Character or a Substring in a String Conversions between Strings and arrays Converting Characters and Numeric Values to Strings Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 3 The String Class Constructing a String: – String message = "Welcome to Java“; – String message = new String("Welcome to Java“); – String s = new String(); Obtaining String length and Retrieving Individual Characters in a string String Concatenation (concat) Substrings (substring(index), substring(start, end)) Comparisons (equals, compareTo) String Conversions Finding a Character or a Substring in a String Conversions between Strings and Arrays Converting Characters and Numeric Values to Strings

java. lang String instructs an empty string String( value: String) Constructs a string with the specified string literal value Constructs a string with the specified character array harAt(index: int ): char Returns the character at the specified index from this string mare To(anotherString: String ):int ompares this string with another string ompare Tolgnore Case(anotherString: String): int Compares this string with another string ignoring case at(another String: String): String ncat this string with another string nds With(suffix: String): boolean Returns true if this string ends with the specified suffix uals(another String: String): boolean returns true if this string is equal to anther string qualslgnore Case(another String: String): boolean Checks if this string equals anther string ignoring case Chars(int srcBegin, int srcEnd, char[] dst, int Copies characters from this string into the destination character h: int): int Returns the index of the first occurrence of indexOf(ch: int, fromIndex: int) int Returns the index of the first occurrence of ch after fromIndex ndexofstr; String): int eturns the index of the first occurrence of str ndexOfstr: String, fromIndex: int ) int Returns the index of the first occurrence of str after fromlndey lastlndexof(ch: int): int Returns the index of the last occurrence of ch Returns the index of the last occurrence of ch before fromIndex lastIndexof(str: String): int Returns the index of the last occurrence of str lastIndexOf(str: String, fromIndex: int ) int Returns the index of the last occurrence of str before fromlnd region Matches(toffset: int, other: String, offset: s true if the specified subregion of thi int, len: int): boolean matches the specified subregion of the string argur Int Returns the number of characters in this string place(old Char:char,newChar:char): String Returns a new string with old Char replaced by newChar ith(prefix: String ): boolean Returns true if this string starts with the specified prefi ubstring(beginIndex: int): String Returns the substring from beginIndex ubString(beginIndex: int, endIndex: int ) String Returns the substring from beginIndex to endIndex-1 CharArray(: charl Returns a char array consisting characters from this string LowerCase(: String Returns a new string with all characters converted to lowercase Returns a new string with itself Upper Case(): String Returns a new string with all characters converted to uppercase mo: String Returns a string with blank characters trimmed on both sides copy ValueOf(data: charl): String Returns a new string consisting of the char array data alueOf data: char): String Same as copy ValueOfidata: charl): String valueofid: double): String eturns a string representing the double value valueof: float): String Returns a string representing the float value Returns a string representing the int value Returns a string representing the long value
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 4 java.lang.String +String() +String(value: String) +String(value: char[]) +charAt(index: int): char +compareTo(anotherString: String): int +compareToIgnoreCase(anotherString: String): int +concat(anotherString: String): String +endsWith(suffix: String): boolean +equals(anotherString: String): boolean +equalsIgnoreCase(anotherString: String): boolean +getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin): void +indexOf(ch: int): int +indexOf(ch: int, fromIndex: int): int +indexOf(str: String): int +indexOf(str: String, fromIndex: int): int +lastIndexOf(ch: int): int +lastIndexOf(ch: int, fromIndex: int): int +lastIndexOf(str: String): int +lastIndexOf(str: String, fromIndex: int): int +regionMatches(toffset: int, other: String, offset: int, len: int): boolean +length(): int +replace(oldChar: char, newChar: char): String +startsWith(prefix: String): boolean +subString(beginIndex: int): String +subString(beginIndex: int, endIndex: int): String +toCharArray(): char[] +toLowerCase(): String +toString(): String +toUpperCase(): String +trim(): String +copyValueOf(data: char[]): String +valueOf(c: char): String +valueOf(data: char[]): String +valueOf(d: double): String +valueOf(f: float): String +valueOf(i: int): String +valueOf(l: long): String Constructs an empty string Constructs a string with the specified string literal value Constructs a string with the specified character array Returns the character at the specified index from this string Compares this string with another string Compares this string with another string ignoring case Concat this string with another string Returns true if this string ends with the specified suffix Returns true if this string is equal to anther string Checks if this string equals anther string ignoring case Copies characters from this string into the destination character array Returns the index of the first occurrence of ch Returns the index of the first occurrence of ch after fromIndex Returns the index of the first occurrence of str Returns the index of the first occurrence of str after fromIndex Returns the index of the last occurrence of ch Returns the index of the last occurrence of ch before fromIndex Returns the index of the last occurrence of str Returns the index of the last occurrence of str before fromIndex Returns true if the specified subregion of this string exactly matches the specified subregion of the string argument Returns the number of characters in this string Returns a new string with oldChar replaced by newChar Returns true if this string starts with the specified prefix Returns the substring from beginIndex Returns the substring from beginIndex to endIndex-1. Returns a char array consisting characters from this string Returns a new string with all characters converted to lowercase Returns a new string with itself Returns a new string with all characters converted to uppercase Returns a string with blank characters trimmed on both sides Returns a new string consisting of the char array data Returns a string consisting of the character c Same as copyValueOf(data: char[]): String Returns a string representing the double value Returns a string representing the float value Returns a string representing the int value Returns a string representing the long value

Constructing Strings String newString=new String(stringLiteral) String message =new String("Welcome to Java") Since strings are used frequently, Java provides a shorthand initializer for creating a string String message =Welcome to Java iang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 5 Constructing Strings String newString = new String(stringLiteral); String message = new String("Welcome to Java"); Since strings are used frequently, Java provides a shorthand initializer for creating a string: String message = "Welcome to Java";

Strings are immutable A String object is immutable: its contents cannot be changed Does the following code change the contents of the string? String s= Java S=HTML iang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 6 Strings Are Immutable A String object is immutable; its contents cannot be changed. Does the following code change the contents of the string? String s = "Java"; s = "HTML";

anIMation Trace Code String s= Java S=HTML After executing String s="Java"; S string String object for"Java" Contents cannot be changed Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 7 Trace Code String s = "Java"; s = "HTML"; : String String object for "Java" s After executing String s = "Java"; After executing s = "HTML"; : String String object for "Java" : String String object for "HTML" Contents cannot be changed This string object is now unreferenced s animation

anIMation Trace Code Strings =Java " HTML After executing s ="HTM String This string object is now unreferenced String object for"Java String String object for"HTML iang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 8 Trace Code String s = "Java"; s = "HTML"; : String String object for "Java" s After executing String s = "Java"; After executing s = "HTML"; : String String object for "Java" : String String object for "HTML" Contents cannot be changed This string object is now unreferenced s animation

Interned Strings Since strings are immutable and are frequentl used, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. Such an instance is called interned. You can also use a string objects intern method to return an interned string For example, the following statements Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 9 Interned Strings Since strings are immutable and are frequently used, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. Such an instance is called interned. You can also use a String object’s intern method to return an interned string. For example, the following statements:

Ex xamples String s ="Welcome to Java String String s1 new String("Welcome to Java")i Interned string object for Welcome to java String s2 s. intern String s3 =Welcome to Javai String System. out. printin ("s1 ==s is +(s1== s) A string object for System. out. printIn("s2==s is +(s2== s) WElcome to Java ystem out println( s3 is +( s3)); display a new object is created if you use the new operator s Is false 02==s is true If you use the string initializer, no new object is created if the interned object is 6 3 is true already created iang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 10 Examples display s1 == s is false s2 == s is true s == s3 is true A new object is created if you use the new operator. If you use the string initializer, no new object is created if the interned object is already created. String s = "Welcome to Java"; String s1 = new String("Welcome to Java"); String s2 = s.intern(); String s3 = "Welcome to Java"; System.out.println("s1 == s is " + (s1 == s)); System.out.println("s2 == s is " + (s2 == s)); System.out.println("s == s3 is " + (s == s3)); : String Interned string object for "Welcome to Java" : String A string object for "Welcome to Java" s s1 s2 s3
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《计算机网络与通讯》课程教学资源(PPT课件讲稿,英文版)Chapter 3 Transport Layer.ppt
- C++ Review.ppt
- 《计算机网络与通讯》课程教学资源(PPT课件讲稿,英文版)Chapter 07 Network Security.ppt
- Incorporating Structured World Knowledge into Unstructured Documents via——Heterogeneous Information Networks.pptx
- FairCloud:Sharing the Network in Cloud Computing.pptx
- 香港科技大学:《计算机网络 Computer Networks》课程教学资源(PPT课件)Chapter 1 Introduction of computer networking.ppsx
- Fluent:《GAMBIT建模教程》教学资源(PPT讲稿)Geometry Operations in GAMBIT.ppt
- 有限元分析 ANSYS:Modeling Turbulent Flows(PPT讲稿)Introductory FLUENT Training.ppt
- 隐马尔科夫模型和词性标注(PPT课件讲稿).ppt
- 哈尔滨工业大学:《中文信息处理》课程教学资源(PPT课件讲稿)句法分析(张宇).ppt
- 新乡学院:《计算机网络》课程教学大纲(适用专业:信息与计算科学).pdf
- 新乡学院:《数据库原理》课程电子教案(PPT课件)第3章 关系数据库.ppt
- 《数据库系统概论 An Introduction to Database System》课程教学资源(PPT课件讲稿)第8讲 数据库恢复技术.ppt
- 河南中医药大学:《网络技术实训》课程教学资源(PPT课件讲稿)第4讲 网络管理实训内容(上).pptx
- 河南中医药大学(河南中医学院):《计算机网络》课程教学资源(PPT课件讲稿)第六章 应用层.ppt
- 《计算机辅助设计——Photoshop制图》课程标准.pdf
- 《操作系统 Operating System》课程电子教案(PPT课件讲稿)第一章 简介.ppt
- 《操作系统》课程教学资源(PPT课件讲稿)文件管理 File Management.ppt
- 《Advanced Artificial Intelligence》课程PPT教学课件(高级人工智能)Lecture 6 Convolutional Neural Network.pptx
- 《Advanced Artificial Intelligence》课程PPT教学课件(高级人工智能)Lecture 3 Decision Tree.pptx
- 印第安纳大学:《Informatics》课程PPT教学课件(信息学)08 网络爬虫 Web Crawling.ppt
- 《操作系统》课程教学资源(PPT课件讲稿)Chapter 1 and 2 Computer System and Operating System Overview.ppt
- 《操作系统》课程教学资源(PPT课件讲稿)Chapter 6 Concurrency Deadlock and Starvation.ppt
- 《操作系统》课程教学资源(PPT课件讲稿)Chapter 8 Virtual Memory.ppt
- 《图像处理与计算机视觉 Image Processing and Computer Vision》课程教学资源(PPT课件讲稿)Chapter 10 Pose estimation by the iterative method.pptx
- Introduction to Internet and TCPIP(PPT讲稿)IP转发 IP FORWARDING.pptx
- GD-Aggregate:A WAN Virtual Topology Building Tool for Hard Real-Time and Embedded Applications.ppt
- 《图像处理与计算机视觉 Image Processing and Computer Vision》课程教学资源(PPT课件讲稿)Chapter 05 Hough transform.pptx
- 香港中文大学:Image processing and computer vision(PPT课件讲稿)Edge detection and image filtering.pptx
- 《图像处理与计算机视觉 Image Processing and Computer Vision》课程教学资源(PPT课件讲稿)Chapter 07 Mean-shift and Cam-shift.pptx
- Essential Cluster OS Commands.ppt
- 香港浸会大学:Kickstart Tutorial/Seminar on using the 64-nodes P4-Xeon Cluster in Science Faculty.ppt
- 香港浸会大学:并行输入输出(PPT讲稿)Parallel I/O.ppt
- 四川大学:《操作系统 Operating System》课程教学资源(PPT课件讲稿)Chapter 7 Memory Management.ppt
- 四川大学:《数据库技术》课程教学资源(PPT课件讲稿)第4章 数据库查询.ppt
- 《计算机系统结构》课程教学资源(PPT课件讲稿)第五章 存储层次.ppt
- 软件配置管理和项目管理工具(PPT讲稿)Software Configuration Management and Project Management Tool.ppt
- 《数据库基础》课程PPT教学课件(SQL Server)第4章 T-SQL与可编程对象.ppt
- 《嵌入式系统开发》课程PPT教学课件(讲稿)第一章 嵌入式系统概述.ppt
- 《编译原理 Compiler Construction》课程教学资源(PPT讲稿)语义分析 Semantic Analysis(Attributes and Attribute Grammars、Algorithms for Attribute Computation).ppt