JDK l http java sun comj 2 se1

  • Slides: 20
Download presentation

JDK 文件網址 l http: //java. sun. com/j 2 se/1. 5. 0/docs/api/java/lang/String. html

JDK 文件網址 l http: //java. sun. com/j 2 se/1. 5. 0/docs/api/java/lang/String. html

String – 屬性 l l l int length() l Returns the length of this

String – 屬性 l l l int length() l Returns the length of this string. char. At(int index) l Returns the char value at the specified index. String substring(int begin. Index) l Returns a new string that is a substring of this string. String substring(int begin. Index, int end. Index) l Returns a new string that is a substring of this string. int index. Of(String str) l Returns the index within this string of the first occurrence of the specified substring. int last. Index. Of(String str) l Returns the index within this string of the rightmost occurrence of the specified substring.

String – 字串比較 l l l boolean equals(Object an. Object) l Compares this string

String – 字串比較 l l l boolean equals(Object an. Object) l Compares this string to the specified object. boolean equals. Ignore. Case(String another. String) l Compares this String to another String, ignoring case considerations. int compare. To(String another. String) l Compares two strings lexicographically. int compare. To. Ignore. Case(String str) l Compares two strings lexicographically, ignoring case differences. boolean starts. With(String prefix) l Tests if this string starts with the specified prefix. boolean ends. With(String suffix) l Tests if this string ends with the specified suffix.

String – 轉換 l l l String replace(char old. Char, char new. Char) l

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

String - 二進位型態轉為字串 l static String value. Of(boolean b) l l static String value.

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

String – 格式化 l static String format(Locale l, String format, Object. . . args)

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

取出頭部 public static String head(String p. Str, String p. Spliter) { int spliter. Pos

取出頭部 public static String head(String p. Str, String p. Spliter) { int spliter. Pos = p. Str. index. Of(p. Spliter); if (spliter. Pos < 0) return p. Str; return p. Str. substring(0, spliter. Pos); }

取出尾部 public static String tail(String p. Str, String p. Spliter) { int spliter. Pos

取出尾部 public static String tail(String p. Str, String p. Spliter) { int spliter. Pos = p. Str. index. Of(p. Spliter); if (spliter. Pos < 0) return ""; return p. Str. substring(spliter. Pos+p. Spliter. length()); }

取出最後部分 public static String last(String p. Str, String p. Spliter) { int spliter. Pos

取出最後部分 public static String last(String p. Str, String p. Spliter) { int spliter. Pos = p. Str. last. Index. Of(p. Spliter); if (spliter. Pos < 0) return p. Str; return p. Str. substring(spliter. Pos+1); }

取出前面部分 public static String no. Last(String p. Str, String p. Spliter) { int spliter.

取出前面部分 public static String no. Last(String p. Str, String p. Spliter) { int spliter. Pos = p. Str. last. Index. Of(p. Spliter); if (spliter. Pos < 0) return p. Str; return p. Str. substring(0, spliter. Pos); }

夾出字串 public static String inner. Text(String p. Xml, String begin. Mark, String end. Mark)

夾出字串 public static String inner. Text(String p. Xml, String begin. Mark, String end. Mark) { int begin. Start = p. Xml. index. Of(begin. Mark); if (begin. Start < 0) return null; int begin. End = begin. Start+begin. Mark. length(); int end. Start = p. Xml. index. Of(end. Mark, begin. End); if (end. Start < 0) return null; return p. Xml. substring(begin. End, end. Start); }

取代特定字串 public static String replace(String p. Str, String from. Pat, String to. Pat) {

取代特定字串 public static String replace(String p. Str, String from. Pat, String to. Pat) { if (from. Pat. length()==0) return p. Str; if (p. Str. index. Of(from. Pat)<0) return p. Str; String. Buffer rz. Str = new String. Buffer(); int str. Idx = 0, next. Idx; while ((next. Idx = p. Str. index. Of(from. Pat, str. Idx))>=0) { rz. Str. append(p. Str. substring(str. Idx, next. Idx)); rz. Str. append(to. Pat); str. Idx = next. Idx + from. Pat. length(); } rz. Str. append(p. Str. substring(str. Idx)); return rz. Str. to. String(); }

連續取代 public static String expand(String p. Text, String p. Macros) { String[] macros =

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

隨堂習題 l 請撰寫一個程式,可以將下列文件轉換成以表格方式 顯示的 HTML 檔案。 <person name="陳鍾誠"> <email>ccc@mail. kmit. edu. tw</email> <url>http: //ccc.

隨堂習題 l 請撰寫一個程式,可以將下列文件轉換成以表格方式 顯示的 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>