1 / 20

Java 與字串處理

Java 與字串處理. 陳鍾誠 2006 年於金門. 字串處理 ?. 電腦資料表達史 二進位  整數、浮點數、布林 …  字串 趨勢 * .doc, *.pdf  *.xml 函數呼叫 (function)  網路呼叫 (SOAP). 二進位傳遞. 優點 佔用空間較少 若雙方格式相同,則不用再轉換 缺點 各種不同的平台與程式語言間的表達方式不統一. 字串傳遞. 優點 人可以讀得懂。 電腦可以用字串比對的方式處理,非常簡單。 跨平台且跨語言 缺點 佔用空間較大 常常需要做字串處理. Java 提供的字串函數.

Télécharger la présentation

Java 與字串處理

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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() • Returns the length of this string. • char charAt(int index) • Returns the char value at the specified index. • String substring(int beginIndex) • Returns a new string that is a substring of this string.  • String substring(int beginIndex, int endIndex) • Returns a new string that is a substring of this string. • int indexOf(String str) • Returns the index within this string of the first occurrence of the specified substring. • int lastIndexOf(String str) • Returns the index within this string of the rightmost occurrence of the specified substring.

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

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

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

  11. String – 格式化 • static Stringformat(Locale l, String format, Object... args) • 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 檔案。 <person name="陳鍾誠"> <email>ccc@mail.kmit.edu.tw</email> <url>http://ccc.kmit.edu.tw/index.htm</url> <job>Assistant Professor in KMIT</job> <education> 學校, 學歷, 入學時間, 畢業時間 NTU, Doctor, 1997/9/1, 2002/7/10 NTU, Master, 1991/9/1, 1993/6/30 NCTU, Bachelor, 1988/9/1, 1992/6/30 </education> </person>

More Related