Java 程式設計 -- 字串
Java 程式簡介運算式分枝迴圈陣列函數遞迴錯誤處理物件導向封裝繼承多型技巧函式庫字串數學正規表達式容器檔案網路資料庫視窗ThreadListener錯誤陷阱相關檔案相關資源教學錄影Eclipse考題解答訊息相關網站參考文獻最新修改簡體版English |
範例:class TestStr1 { public static void main(String[] args) { System.out.println("===字串的宣告==="); String s1 = "abcd"; String s2 = new String("abcd"); String s3 = new String("xyz"); System.out.println("s1="+s1); System.out.println("s2="+s2); System.out.println("s3="+s3); System.out.println("\r\n===字串的宣告==="); System.out.println("s1.length()="+s1.length()); System.out.println("\r\n===字串的比較==="); if (s1.equals(s2)) System.out.println("s1.equals(s2)"); else System.out.println("not s1.equals(s2)"); if (s1 == s2) System.out.println("s1 == s2"); else System.out.println("not s1 == s2"); } } 範例class TestStr2 { public static void main(String[] args) { System.out.println("===字串宣告==="); String s1 = "abcdefg"; String s2 = "cde"; System.out.println("s1="+s1); System.out.println("s2="+s2); System.out.println("\r\n===尋找子字串==="); int i1 = s1.indexOf(s2); System.out.println(s1+".indexOf("+s2+")="+i1); System.out.println("\r\n===取代字元==="); s1 = s1.replace('d', 'x'); System.out.println("s1.replace('d','x')="+s1); System.out.println("\r\n===再次尋找子字串==="); int i2 = s1.indexOf(s2); System.out.println(s1+".indexOf("+s2+")="+i2); System.out.println("\r\n===字串分割==="); String s3 = "abc,def,ghij,klmnop"; System.out.println("s3="+s3); System.out.println("cut=s3.split(,)"); String cut[] = s3.split(","); for (int i=0; i<cut.length; i++) System.out.println("cut["+i+"]="+cut[i]); } } |
page revision: 1, last edited: 04 Nov 2010 05:17
Post preview:
Close preview