Lecture Introduction to Java programming - Chapter 9: Strings and text I/O

Often you encounter the problems that involve string processing and file input and output. Suppose you need to write a program to replace all occurrences of a word with a new word in a file. How do you solve this problem? This chapter introduces strings and text files, which will enable you to solve this problem. | Chapter 9 Strings and Text I/O Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write a program to replace all occurrences of a word with a new word in a file. How do you solve this problem? This chapter introduces strings and text files, which will enable you to solve this problem. Objectives To use the String class to process fixed strings (§). To use the Character class to process a single character (§). To use the StringBuilder/StringBuffer class to process flexible strings (§). To distinguish among the String, StringBuilder, and StringBuffer classes (§). To learn how to pass arguments to the main method from the command line (§). To discover file properties and to delete and rename files using the File class (§). To write data to a file using the PrintWriter class (§). To read data from a file using the Scanner class (§). (GUI) To open files using a dialog box (§). 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 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"; Trace Code String s = "Java"; s = "HTML"; animation Trace Code | Chapter 9 Strings and Text I/O Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write a program to replace all occurrences of a word with a new word in a file. How do you solve this problem? This chapter introduces strings and text files, which will enable you to solve this problem. Objectives To use the String class to process fixed strings (§). To use the Character class to process a single character (§). To use the StringBuilder/StringBuffer class to process flexible strings (§). To distinguish among the String, StringBuilder, and StringBuffer classes (§). To learn how to pass arguments to the main method from the command line (§). To discover file properties and to delete and rename files using the File class (§). To write data to a file using the PrintWriter class (§). To read data from a file using the Scanner class (§). (GUI) To open files using a dialog box (§).

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.