Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java 與字串處理 陳鍾誠 2006 年於金門. 字串處理 ? 電腦資料表達史 二進位  整數、浮點數、布林 …  字串 趨勢 *.doc, *.pdf  *.xml 函數呼叫 (function)  網路呼叫 (SOAP)

Similar presentations


Presentation on theme: "Java 與字串處理 陳鍾誠 2006 年於金門. 字串處理 ? 電腦資料表達史 二進位  整數、浮點數、布林 …  字串 趨勢 *.doc, *.pdf  *.xml 函數呼叫 (function)  網路呼叫 (SOAP)"— Presentation transcript:

1 Java 與字串處理 陳鍾誠 2006 年於金門

2 字串處理 ? 電腦資料表達史 二進位  整數、浮點數、布林 …  字串 趨勢 *.doc, *.pdf  *.xml 函數呼叫 (function)  網路呼叫 (SOAP)

3 二進位傳遞 優點 佔用空間較少 若雙方格式相同,則不用再轉換 缺點 各種不同的平台與程式語言間的表達方式不統一

4 字串傳遞 優點 人可以讀得懂。 電腦可以用字串比對的方式處理,非常簡單。 跨平台且跨語言 缺點 佔用空間較大 常常需要做字串處理

5 Java 提供的字串函數

6 JDK 文件網址 http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

7 String – 屬性 int length()length Returns the length of this string. char charAt(int index)charAt Returns the char value at the specified index. String substring(int beginIndex)substring Returns a new string that is a substring of this string. String substring(int beginIndex, int endIndex)substring Returns a new string that is a substring of this string. int indexOf(String str)indexOfString Returns the index within this string of the first occurrence of the specified substring. int lastIndexOf(String str)lastIndexOfString Returns the index within this string of the rightmost occurrence of the specified substring.

8 String – 字串比較 boolean equals(Object anObject)equalsObject Compares this string to the specified object. boolean equalsIgnoreCase(String anotherString)equalsIgnoreCaseString Compares this String to another String, ignoring case considerations. int compareTo(String anotherString)compareToString Compares two strings lexicographically. int compareToIgnoreCase(String str)compareToIgnoreCaseString Compares two strings lexicographically, ignoring case differences. boolean startsWith(String prefix)startsWithString Tests if this string starts with the specified prefix. boolean endsWith(String suffix)endsWithString Tests if this string ends with the specified suffix.

9 String – 轉換 String replace(char oldChar, char newChar)replace Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar String toLowerCase()toLowerCase Converts all of the characters in this String to lower case using the rules of the default locale. String toUpperCase()toUpperCase Converts all of the characters in this String to upper case using the rules of the default locale. String trim()trim Returns a copy of the string, with leading and trailing whitespace omitted. String[] split(String regex)splitString Splits this string around matches of the given regular expression.

10 String - 二進位型態轉為字串 static String valueOf(boolean b)StringvalueOf Returns the string representation of the boolean argument static String valueOf(char c)StringvalueOf Returns the string representation of the char argument. static String valueOf(char[] data)StringvalueOf Returns the string representation of the char array argument. static String valueOf(char[] data, int offset, int count)StringvalueOf Returns the string representation of a specific subarray of the char array argument. static String valueOf(double d)StringvalueOf Returns the string representation of the double argument. static String valueOf(float f)StringvalueOf Returns the string representation of the float argument. static String valueOf(int i)StringvalueOf Returns the string representation of the int argument. static String valueOf(long l)StringvalueOf Returns the string representation of the long argument. static String valueOf(Object obj)StringvalueOfObject Returns the string representation of the Object argument.

11 String – 格式化 static String format(Locale l, String format, Object... args)StringLocaleStringObject Returns a formatted string using the specified locale, format string, and arguments. Ex: String t1 = String.format(Locale.TAIWAN, "(%4$2s %3$2s %2$2s %1$2s)", "a", "b", "c", "d"); //  t1 = " d c b a" String t2 = String.format(Locale.FRANCE, "e = %+10.4f", Math.E); // -> "e = +2,7183"

12 我所常用的字串函數

13 取出頭部 public static String head(String pStr, String pSpliter) { int spliterPos = pStr.indexOf(pSpliter); if (spliterPos < 0) return pStr; return pStr.substring(0,spliterPos); }

14 取出尾部 public static String tail(String pStr, String pSpliter) { int spliterPos = pStr.indexOf(pSpliter); if (spliterPos < 0) return ""; return pStr.substring(spliterPos+pSpliter.length()); }

15 取出最後部分 public static String last(String pStr, String pSpliter) { int spliterPos = pStr.lastIndexOf(pSpliter); if (spliterPos < 0) return pStr; return pStr.substring(spliterPos+1); }

16 取出前面部分 public static String noLast(String pStr, String pSpliter) { int spliterPos = pStr.lastIndexOf(pSpliter); if (spliterPos < 0) return pStr; return pStr.substring(0, spliterPos); }

17 夾出字串 public static String innerText(String pXml, String beginMark, String endMark) { int beginStart = pXml.indexOf(beginMark); if (beginStart < 0) return null; int beginEnd = beginStart+beginMark.length(); int endStart = pXml.indexOf(endMark, beginEnd); if (endStart < 0) return null; return pXml.substring(beginEnd, endStart); }

18 取代特定字串 public static String replace(String pStr, String fromPat, String toPat) { if (fromPat.length()==0) return pStr; if (pStr.indexOf(fromPat)<0) return pStr; StringBuffer rzStr = new StringBuffer(); int strIdx = 0, nextIdx; while ((nextIdx = pStr.indexOf(fromPat, strIdx))>=0) { rzStr.append(pStr.substring(strIdx, nextIdx)); rzStr.append(toPat); strIdx = nextIdx + fromPat.length(); } rzStr.append(pStr.substring(strIdx)); return rzStr.toString(); }

19 連續取代 public static String expand(String pText, String pMacros) { String[] macros = pMacros.split("\\|"); for (int i=0; i<macros.length; i++) { String name = head(macros[i], "="); String expand = tail(macros[i], "="); pText = replace(pText, name, expand); } return pText; }

20 隨堂習題 請撰寫一個程式,可以將下列文件轉換成以表格方式 顯示的 HTML 檔案。 ccc@mail.kmit.edu.tw http://ccc.kmit.edu.tw/index.htm Assistant Professor in KMIT 學校, 學歷, 入學時間, 畢業時間 NTU,Doctor,1997/9/1,2002/7/10 NTU,Master,1991/9/1,1993/6/30 NCTU,Bachelor,1988/9/1,1992/6/30


Download ppt "Java 與字串處理 陳鍾誠 2006 年於金門. 字串處理 ? 電腦資料表達史 二進位  整數、浮點數、布林 …  字串 趨勢 *.doc, *.pdf  *.xml 函數呼叫 (function)  網路呼叫 (SOAP)"

Similar presentations


Ads by Google