200 likes | 315 Views
Java 與字串處理. 陳鍾誠 2006 年於金門. 字串處理 ?. 電腦資料表達史 二進位 整數、浮點數、布林 … 字串 趨勢 * .doc, *.pdf *.xml 函數呼叫 (function) 網路呼叫 (SOAP). 二進位傳遞. 優點 佔用空間較少 若雙方格式相同,則不用再轉換 缺點 各種不同的平台與程式語言間的表達方式不統一. 字串傳遞. 優點 人可以讀得懂。 電腦可以用字串比對的方式處理,非常簡單。 跨平台且跨語言 缺點 佔用空間較大 常常需要做字串處理. Java 提供的字串函數.
E N D
Java 與字串處理 陳鍾誠 2006 年於金門
字串處理? • 電腦資料表達史 • 二進位 整數、浮點數、布林 … 字串 • 趨勢 • *.doc, *.pdf *.xml • 函數呼叫 (function) 網路呼叫 (SOAP)
二進位傳遞 • 優點 • 佔用空間較少 • 若雙方格式相同,則不用再轉換 • 缺點 • 各種不同的平台與程式語言間的表達方式不統一
字串傳遞 • 優點 • 人可以讀得懂。 • 電腦可以用字串比對的方式處理,非常簡單。 • 跨平台且跨語言 • 缺點 • 佔用空間較大 • 常常需要做字串處理
JDK 文件網址 • http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
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.
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.
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.
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.
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"
取出頭部 public static String head(String pStr, String pSpliter) { int spliterPos = pStr.indexOf(pSpliter); if (spliterPos < 0) return pStr; return pStr.substring(0,spliterPos); }
取出尾部 public static String tail(String pStr, String pSpliter) { int spliterPos = pStr.indexOf(pSpliter); if (spliterPos < 0) return ""; return pStr.substring(spliterPos+pSpliter.length()); }
取出最後部分 public static String last(String pStr, String pSpliter) { int spliterPos = pStr.lastIndexOf(pSpliter); if (spliterPos < 0) return pStr; return pStr.substring(spliterPos+1); }
取出前面部分 public static String noLast(String pStr, String pSpliter) { int spliterPos = pStr.lastIndexOf(pSpliter); if (spliterPos < 0) return pStr; return pStr.substring(0, spliterPos); }
夾出字串 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); }
取代特定字串 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(); }
連續取代 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; }
隨堂習題 • 請撰寫一個程式,可以將下列文件轉換成以表格方式顯示的 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>