20
Java 與與與與與 陳陳陳 2006 陳陳陳陳

Java 與字串處理

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Java  與字串處理

Java 與字串處理

陳鍾誠 2006 年於金門

Page 2: Java  與字串處理

字串處理 ?

電腦資料表達史 二進位 整數、浮點數、布林 … 字串

趨勢 *.doc, *.pdf *.xml 函數呼叫 (function) 網路呼叫 (SOAP)

Page 3: Java  與字串處理

二進位傳遞 優點

佔用空間較少 若雙方格式相同,則不用再轉換

缺點 各種不同的平台與程式語言間的表達方式不統一

Page 4: Java  與字串處理

字串傳遞 優點

人可以讀得懂。 電腦可以用字串比對的方式處理,非常簡單。 跨平台且跨語言

缺點 佔用空間較大 常常需要做字串處理

Page 5: Java  與字串處理

Java 提供的字串函數

Page 6: Java  與字串處理

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

Page 7: Java  與字串處理

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.

Page 8: Java  與字串處理

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 difference

s. 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.

Page 9: Java  與字串處理

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 th

e 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.

Page 10: Java  與字串處理

String - 二進位型態轉為字串 static String valueOf(boolean b)

Returns the string representation of the boolean argument static String valueOf(char c)

Returns the string representation of the char argument. static String valueOf(char[] data)

Returns the string representation of the char array argument. static String valueOf(char[] data, int offset, int count)

Returns the string representation of a specific subarray of the char array argument. static String valueOf(double d)

Returns the string representation of the double argument. static String valueOf(float f)

Returns the string representation of the float argument. static String valueOf(int i)

Returns the string representation of the int argument. static String valueOf(long l)

Returns the string representation of the long argument. static String valueOf(Object obj)

Returns the string representation of the Object argument.

Page 11: Java  與字串處理

String – 格式化 static String format(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"

Page 12: Java  與字串處理

我所常用的字串函數

Page 13: Java  與字串處理

取出頭部

public static String head(String pStr, String pSpliter) {

int spliterPos = pStr.indexOf(pSpliter);

if (spliterPos < 0) return pStr;

return pStr.substring(0,spliterPos);

}

Page 14: Java  與字串處理

取出尾部

public static String tail(String pStr, String pSpliter) {

int spliterPos = pStr.indexOf(pSpliter);

if (spliterPos < 0) return "";

return pStr.substring(spliterPos+pSpliter.length());

}

Page 15: Java  與字串處理

取出最後部分

public static String last(String pStr, String pSpliter) {

int spliterPos = pStr.lastIndexOf(pSpliter);

if (spliterPos < 0) return pStr;

return pStr.substring(spliterPos+1);

}

Page 16: Java  與字串處理

取出前面部分

public static String noLast(String pStr, String pSpliter) {

int spliterPos = pStr.lastIndexOf(pSpliter);

if (spliterPos < 0) return pStr;

return pStr.substring(0, spliterPos);

}

Page 17: Java  與字串處理

夾出字串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);

}

Page 18: Java  與字串處理

取代特定字串 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();

}

Page 19: Java  與字串處理

連續取代

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; }

Page 20: Java  與字串處理

隨堂習題 請撰寫一個程式,可以將下列文件轉換成以表格方式

顯示的 HTML 檔案。<person name="陳鍾誠 "> <email>[email protected]</email> <url>http://ccc.kmit.edu.tw/index.htm</url> <job>Assistant Professor in KMIT</job> <education>學校 , 學歷 , 入學時間 , 畢業時間NTU, Doctor, 1997/9/1, 2002/7/10NTU, Master, 1991/9/1, 1993/6/30NCTU, Bachelor, 1988/9/1, 1992/6/30 </education></person>