String and Text I/O 2010.12.17. Outline The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters,

Slides:



Advertisements
Similar presentations
Java
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
第六章 数组与字符串 §6.1 数组 §6.2 字符串. §6.1 数组 数组的创建 ( 一维数组和二维数组;基本数据类型和 复合数据类型) 1. 指定数组名称、数据类型 type var_name[]; 如: char s[]; Object o[]; int i[][];
作業 : list  stack struct node {int info; node* next; }; class list { node *head, *tail ; int node_no ; public: list() ;list(const node& n) ;list(const.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
6-1 指標簡介 6-2 指標與陣列 6-3 動態配置記憶體 6-4 本章綜合練習
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings and Text.
Instructor: Ching-Chi Lin 林清池 助理教授
Programming 2 CS112- Lab 2 Java
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
Graph V(G 1 )={0, 1, 2, 3, 4, 5, 6, 7, 8, 9} E(G 1 )={(0, 2), (0, 3), (1, 4), (2, 3), (2, 5), (2, 6), (3, 6), (3, 7), (4, 7), (5, 6), (5,
Multidimensional Array Outline Two-Dimensional Arrays Lengths of Two-Dimensional Arrays Multidimension Arrays [Sample code]
 Pearson Education, Inc. All rights reserved break and continue Statements Break and continue statements – Alter flow of control break.
Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009.
Thinking in Objects Outline The this Reference [Sample code] PressEnter.java.
7 陣列與搜尋 7.1 陣列 一般資料變數 宣告一維陣列 起始一維陣列 7-4
Hung-Hsiang WuWindows Processing Design1 Chapter 3 基本觀念 變數宣告與型態 特殊運算子符號 字串與數值的轉換 類別與物件的觀念 建立新的專案 WinMain 程式進入點 Include Header File.
Binary I/O Outline Text I/O Chap 9. WriteData Chap 9. ReadData Binary I/O Text I/O vs Binary I/O Binary I/O Classes Using DataInputStream/DataOutputStream.
資料結構實習-一 參數傳遞.
參考書籍:古頤榛, Visual C++ 6教學範本 , 碁峰資訊股份有限公司。
1/46 物件導向的字串處理 內建的完整類別 string 具有豐富的 成員函數,能以物件導向的語法處 理字串,讓程式設計者免除空間安 排及安插結尾符號等瑣碎的工作。 內建的完整類別 string 具有豐富的 成員函數,能以物件導向的語法處 理字串,讓程式設計者免除空間安 排及安插結尾符號等瑣碎的工作。
資料結構實習-二.
Fundamental Programming Structures in Java: Strings.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
資料結構實習-十 Binary Tree Traversal.
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
OBJECTS AND CLASSES Outline Reference Variables and Reference Types Primitive Types and Reference Types Static Variable, Constants,
2 C++ 程式概論 2.1 C++ 程式結構 程式註解 // 插入標題檔 #include 2-3
Java class Outline Defining a method Calling Method Passing parameters [Sample code] TestMethod.java 、 TestMethod2.java 、 GCD.java 、 prime.java.
Visual C++重點複習.
資料結構實習-六.
11 Ch05 遞迴 淡江大學 周清江 1. 2  遞迴函數乃是一個自己反覆呼叫自己的函數  一個典型的遞迴演算法 n! = n * (n-1)! = n * (n-1) * (n-2)! = n * (n-1) * (n-2) * (n-3)! = … = n * (n-1) * (n-2)
C 語言練習題 2010/12/2. C 程式的格式 一、 C 程式的格式 (1). /* …. */ 是 C 程式的 ______ main() 的內容是由敘述構成的 (2). 敘述要以 __ 為結束符號 (3). 變數、函數都要做 ____ 的宣告 (4). ‘=’ 是 ____ 用的算符 (5).
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
String & Composition. Agenda This keyword. String class. String operations. Composition.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 8 Strings 1.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,
CHAPTER 8 File Input Output Part 1: String. The String Class  Constructing a String: String message = "Welcome to Java“; String message = new String("Welcome.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
3 - 1 Text Processing In Java: Characters and Strings Reading:Downey: Chapter 7 Problem Set:Assignment #1 due Tuesday, Feburary 13 Wellesley College CS230.
String Definition A string is a set of characters that behaves as a single unit. The characters in a string include upper-case and lower- case letters,
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
Lab String Concatenation String s3 = s1.concat(s2); String s3 = s1 + s2; s1 + s2 + s3 + s4 + s5 same as (((s1.concat(s2)).concat(s3)).concat(s4)).concat(s5);
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Strings.
Programming in Java Text Books :
F4105 JAVA PROGRAMMING F4105 Java Programming
Chapter 7: Strings and Characters
Chapter 9 Strings and Text I/O
Lecture 07 String Jaeki Song.
Unit-2 Objects and Classes
Presentation transcript:

String and Text I/O

Outline The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings Converting, replaceing, and Splitting Strings Finding a Character or a Substring in a String The File Class [Sample code] TestFileClass.java

The String Class A string is a sequence of characters. The String class has 11 constructors and more than 40 methods. Declaration: 1.String S = new String(“Hello Java”); 2.String S = “Hello Java”; 3.Char[] C = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ’ ’, ‘J’, ‘a’, ‘v’, ‘a’ }; String S = new String(C);

Immutable Strings and Interned Strings A string object is immutable; its content cannot be changed. String object for “Java” :StringS String object for “Java” :StringS String object for “html” :String S = “html”;String S = “Java”; X

S1.equals(S2): boolean S1.equalsIgnoreCase(S2): boolean S1.compareTo(S2): int S1.compareToIgnoreCase(S2): int. Java.lang.String 推薦網站 : String Comparisons ( 課本 p.327)

String Length, Characters, and Combining Strings HelloJava S.charAt(0) Java.lang.String S.length(): int S.charAt(index: int): char S.concat(S2): String S.length() = 10 S.charAt(9) A String object is presented using an array internally. ( 課本 p.329)

程式練習 : 1. 宣告一個 String object S1 = “HELLO JAVA ” 2. 宣告一個 String object S2 = “i love Java” 3. 印出 S1 的長度 4. 印出 S1 的第 7 個 char 的內容 5. 用 equals 比較 S1 和 S2, 並印出 true 或 false 6. 用 concat 將 S1 和 S2 字串連接起來, 並印出

Obtaining Substrings Java.lang.String S.substring(beginIndex: int): String S.substring(beginIndex: int, endIndex: int): String HelloJava S.substring(0,5) ( 課本 p.330)

程式練習 : 使用 substring method, 印出 S1 中的第 0~4 個 characters

Converting, replaceing, and Splitting Strings Java.lang.String S.toLowerCase(): String S.toUpperCase(): String S.trim():String S.replace(oldchar, newchar): String S.replaceFirst(oldString, newString): String S.replaceAll(oldString, newString): String S.split (delimiter: String): String[ ] ( 課本 p.331)

程式練習 : 1. 將 S1 轉成 lowercase 印出 2. 將 S2 轉成 uppercase 印出 3. 將 S1 中的 ”JAVA” 子字串 換成 ”C++” 印出 4. 嘗試字串分割 String[] names = S1.split(" "); for(String name:names) { System.out.println(name); }

Finding a Character or a Substring in a String Java.lang.String “JAVA”.indexOf(‘J’) return 0. “JAVA”.indexOf(‘A’) return 1. “JAVA”.indexOf(‘A’,2) return 3. “JAVA”.indexOf(”VA”) return 2. “JAVA”.indexOf(”ABC”,2) return -1. “JAVA”.lastIndexOf(‘A’) return 3.. ( 課本 p.332)

程式練習 : 從 S1 找出子字串 ”JAVA” 印出回傳值

The File Class Create File object. EX: java.io.File file = new java.io.File(“abc.txt”); Writing Data Using PrintWriter. EX: PrintWriter output = new PrintWriter(file); ( 課本 p.349)

程式練習 :  練習 p.349 Listing 9.7 WriteData.java

程式範例 :  TestFileClass.java