Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 字串與時間處理.

Similar presentations


Presentation on theme: "Chapter 6 字串與時間處理."— Presentation transcript:

1 Chapter 6 字串與時間處理

2 String 類別 String 變數名稱; 變數名稱 = new String(“字串內容”); or
建構子 String 變數名稱; 變數名稱 = new String(“字串內容”); or String 變數名稱 = new String(“字串內容”); String 變數名稱 = “字串內容”;

3 String 類別 位於 java.lang 套件中;TOMCAT及RESIN的JSP engine 已預設引用此套件。因此不需運用 import 屬性,引用該套件。

4 字串的連接 <% String str; String str1 = new String(“您好”);
str = str1 + str2; out.println(str); %>

5 字串的比較 二字串是否指向同一個物件 二字串內容是否相符 利用 == 運算子 利用String類別的equals方法

6 Example <% String str1 = new String(“您好”);
String str3 = str1; if (str1==str2) out.println(“同一字串物件”); else out.println(“非同一字串物件”); if (str1.equals(str2)) out.println(“內容相同”); out.println(“內容不同”); if (str1 == str3) if (str1.equals(str3)) %>

7 Literal strings 的比較 只要在同一個package裡的同一個class中的literal strings的內容相同的話,就會指向同一個String Object String str1=“你好”; String str2=“你好”; If (str1==str2) out.print(“same”); Else out.print(“different”); same

8 字串長度的取得 字串變數.length(); String 的方法 傳回值為int

9 大小寫的轉換 toLowCase() 方法 toUpperCase() 方法 str.toLowCase();
str.toUpperCase();

10 字串的擷取 substring(int startIndex) 方法
substring(int startIndex, int endIndex) 方法 <% String str = “您好歡迎光臨本網站”; String subStr; subStr = str.substring(3,6); out.println(subStr); %> 擷取子字串最後一個字元 後的字元索引值 index 由 0 開始計算 您好歡迎光臨本網站! index

11 字元或字串的搜尋 indexOf(char ch) 方法 indexOf(String substr) 方法 <%
out.println(str.indexOf(‘!’)); %> <%=str.indexOf(“光臨”)%> 回傳第一次出現在字串中的位置(起始位置為0); 傳回值資料型態為int,若找不到則回傳-1。

12 字元取代 replace(char oldChar, char newChar) 方法 <%
String str = new String(“Hello!你好”); String newStr; newStr = str.replace(‘!’,’.’); out.println(newStr); %>

13 部分字串取代技巧 <% String str = new String(“我是你的老師”);
String newStr = new String(“學生”); String repStr = new String(“老師”); int find = -1; int replen = repStr.length(); do { find = str.indexOf(repStr); if (find != -1) str = str.substring(0,find)+newStr+str.substring(find+replen); } while(find != -1); out.println(str); %> Note:新版本 java 提供 replaceAll(String, String) 方法,完成部份字串取代

14 Date 類別 java 中對日期/時間的處理 於java.util package中定義=>需事先import
以1970/1/1 00:00:00 為基準 Date 變數名稱 = new Date(); Date 變數名稱 = new Date(long 毫秒數); Date 變數名稱 = new Date(int year, int month, int day); Date 變數名稱 = new Date(int year, int month, int day, int hour, int minute); Date 變數名稱 = new Date(int year, int month, int day, int hour, int minute, int second); Note: year 以 1900 為基準 month 輸入 0~11; 0 代表 1 月

15 時間間隔計算 getTime()方法=>取得距1970/1/1 00:00:00 毫秒數 <%
Date date1 = new Date(); Date date2 = new Date(90,1,1); long timediff = date1.getTime()-date2.getTime(); out.println(“兩時間差為”+timediff+”毫秒”); out.println(“兩時間差為”+timediff/1000+” 秒”); out.println(“兩時間差為”+timediff/60000+”分”); out.println(“兩時間差為”+timediff/ ”小時”); out.println(“兩時間差為”+timediff/ /24+”天”); %>

16 時間欄位的取得 getYear() 方法 getMonth() 方法 getDate() 方法 – 取得日
getDay() 方法 – 取得星期 getHours() 方法 getMinutes() 方法 getSeconds() 方法

17 時間欄位的設定 setYear(int year) 方法 setMonth(int month) 方法 – 0~11
setDate(int date) 方法 setHours(int hours) 方法 setMinutes(int minutes) 方法 setSeconds(int seconds) 方法


Download ppt "Chapter 6 字串與時間處理."

Similar presentations


Ads by Google