java

 

 

publicclass first_Java {

                publicstaticvoid main(String[] args) {

                     char ch1[] = { 'h', 'e', 'l', 'l', 'o' };// 宣告字元陣列

                     String s1 = "How are you";// 宣告基本型態字串

                     String s2 = new String("I am fine,thanks"); // 建立字串類別物件並初始化

                     String str1 = new String(ch1);

                     String str2 = new String(ch1, 2, 3);

                     String str3 = new String(s1);

                     String str4 = new String(s2);

                     System.out.println("以字元陣列作參數建立字串的內容:" + str1);

                     System.out.println("以字元陣列並指定字數建立字串的內容:" + str2);

                     System.out.println("以字串作參數建立字串的內容:" + str3);

                     System.out.println("以字串物件作參數建立字串的內容:" + str4);

                }

}

 

 

以字元陣列作參數建立字串的內容:hello

以字元陣列並指定字數建立字串的內容:llo

以字串作參數建立字串的內容:How are you

以字串物件作參數建立字串的內容:I am fine,thanks

 

----------------

 

 

publicclass first_Java {

                publicstaticvoid main(String[] args){

                    

                    //字元陣列建構法建立字串

                     char a[]={'I','L','o','v','e','j','a','v','a'};  //建立字元陣列

                     String str1=new String(a);   

                     String str2=new String(a,5,4);    

                     System.out.println("完整顯示字元陣列a:"+str1);

                     System.out.println("只顯示a[5]之後的4個字元:"+str2+'\n');

                    

                     char b[]={'I',' ','L','o','v','e',' ','J','a','v','a'};  //建立字元陣列

                     String str3=new String(b);   

                     String str4=new String(b,6,5);    

                     System.out.println("完整顯示字元陣列b:"+str3);

                     System.out.println("只顯示a[6]之後的4個字元:"+str4);

 

                }

}

 

 

完整顯示字元陣列a:ILovejava

只顯示a[5]之後的4個字元:java

 

完整顯示字元陣列b:I Love Java

只顯示a[6]之後的4個字元: Java

 

 

--------------

 

 

 

 

publicclass first_Java {

               publicstaticvoid main(String[] args){

                    

                     String str="天助自助者"; //建立字串物件並初始化

                     System.out.println("字串:"+str);

                     System.out.println("常用方式:lenght()");

                     System.out.println("字串長度:"+str.length()+'\n');

                    

                     //另一種計算字串物件的方法:字面法

                     System.out.println("字面法:");

                     int a="天助自助者".length();

                     System.out.println("字串長度:"+a);

               }

}

 

 

字串:天助自助者

常用方式:lenght()

字串長度:5

 

字面法:

字串長度:5

 

-------------

 

import java.io.*;

publicclass first_Java {

                 publicstaticvoid main(String[] args)throws Exception{

                                String str1="Time and Tide wait for no man.";

                                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//定義從鍵盤輸入輸入

                                System.out.println("str1:"+str1);

                                String s1;

                         System.out.print("請輸入要搜尋的字串:");

                                s1=br.readLine();//從鍵盤數入字串

                                int index=0;                             

                                int len=str1.length();

                         for(int i=1; i<len;i++){

                                           index =str1.indexOf(s1,index);

                         }

                                System.out.println("在"+index+"位置找到搜尋的字串");                         

                }

}

 

 

str1:Time and Tide wait for no man.

請輸入要搜尋的字串:aacc

在-1位置找到搜尋的字串

 

-1位置表示找不到)

------------

 

str1:Time and Tide wait for no man.

請輸入要搜尋的字串:and

在5位置找到搜尋的字串

 

------------

 

 

 

 

 

 

publicclass first_Java {

               publicstaticvoid main(String[] args){

                    

                     String str="Happy Birthday to you";

                    

                     //取代字串

                     String str_new=str.replace("you","Joe");

                     System.out.println("取代前:"+str);

                     System.out.println("取代後:"+str_new+'\n');

                    

                     //去除空白部份

                     String str2="   Happy Birthday to you  ";

                     System.out.println("去除空白前,字串長度:"+str2.length());

        String str2_new=str.trim();

                     System.out.println("去除前:"+str2);

                     System.out.println("去除後:"+str2_new);

                     System.out.println("去除空白後,字串長度:"+str2_new.length());

               }   

}

 

 

取代前:Happy Birthday to you

取代後:Happy Birthday to Joe

 

去除空白前,字串長度:26

去除前:   Happy Birthday to you 

去除後:Happy Birthday to you

去除空白後,字串長度:21

 

(去除空白是前後的空白都會去除)

------------

 

 

publicclass first_Java {

                publicstaticvoid main(String[] args){

                    

                     String str1="Java2";

                     String str2="Java2";

                     String str3="JAVA2";

                    

                     //比較字串是否相同

                     boolean a1=str1.equals(str2);

                     boolean a2=str2.equals(str3);

                     boolean a3=str3.equals(str1);

                    

                     //比較字串是否相同,但忽略大小寫

                     boolean b1=str1.equalsIgnoreCase(str2);

                     boolean b2=str2.equalsIgnoreCase(str3);

                     boolean b3=str3.equalsIgnoreCase(str1);

                    

                     //完整比較

                     int c1=str1.compareTo(str2);

                     int c2=str2.compareTo(str3);

                     int c3=str3.compareTo(str1);

                    

                     //比較字串開頭

                     boolean d1=str1.startsWith("Ja");

                     boolean d2=str2.startsWith("Ja");

                     boolean d3=str3.startsWith("Ja");

                    

                     //比較字串結尾

                     boolean e1=str1.endsWith("a2");

                     boolean e2=str2.endsWith("a2");

                     boolean e3=str3.endsWith("A2");

 

                     System.out.println("比較字串是否相同:"+a1+" "+a2+" "+a3+'\n');

                     System.out.println("比較字串是否相同,但忽略大小寫:"+b1+" "+b2+" "+b3+'\n');

                     System.out.println("完整比較:"+c1+" "+c2+" "+c3+'\n');

                     System.out.println("比較字串開頭:"+d1+" "+d2+" "+d3+'\n');

                     System.out.println("比較字串結尾:"+e1+" "+e2+" "+e3+'\n');

                }

}

 

 

比較字串是否相同:true false false

 

比較字串是否相同,但忽略大小寫:true true true

 

完整比較:0 32 -32

 

比較字串開頭:true true false

 

比較字串結尾:true true true

 

 

--------

 

 

publicclass first_Java {

              publicstaticvoid main(String[] args){

                   

                     String str=new String("Time creates Hero");//宣告字串

                     System.out.println("原來的字串:"+str);    

                     System.out.println("轉換後的字串: "+str.toUpperCase());

                     char[] ch={'S','t','r','i','n','g',' ','a','r','r','a','y'};//宣告字元陣列

                     System.out.println("將字元陣列轉換成字串:"+str.copyValueOf(ch, 7, 5));

                     double a=78.54;//宣告double數字

                     System.out.println("將數字轉換成字串:"+String.valueOf(a));

               }   

}

 

 

 

原來的字串:Time creates Hero

轉換後的字串: TIME CREATES HERO

將字元陣列轉換成字串:array

將數字轉換成字串:78.54

 

 

 

---------

 

 

 

 

 

 

 

 

 

 

 

publicclass first_Java {

                publicstaticvoid main(String[] args) {

                     // 宣告字串物件

                     String str1 = "Power";

                     String str2 = "Point";

 

                     // 串接字串方式一

                     String str3 = str1 + str2;

                      String str4 = "Power" + "Point";

 

                     // 串接字串方式二

                     String str5 = str1.concat(str2);

                     String str6 = str1.concat("Point");

 

                     System.out.println("串接字串方式一:");

                     System.out.println(str3);

                     System.out.println(str4);

                     System.out.println();

 

                     System.out.println("串接字串方式二:");

                     System.out.println(str5);

                     System.out.println(str6);

 

                }

}

 

 

串接字串方式一:

PowerPoint

PowerPoint

 

串接字串方式二:

PowerPoint

PowerPoint

 

 

-------------

 

 

publicclass first_Java {

                publicstaticvoid main(String[] args){

                     // 宣告字串物件

                     String str1="Java Script";

                     System.out.println(str1+'\n');  //'\n'是換行

                    

                     //擷取指定位置的字元

                     char a1=str1.charAt(5);

                     char a2=str1.charAt(4);

                     System.out.println("指定位置[5],並取得其字元是:"+a1);

                     System.out.println("指定位置[4],並取得其字元是:"+a2+'\n');

                    

                     //取得子字串

                     String str2=str1.substring(5,11);

                     System.out.println("指定取得字串的範圍,(5,11)其子字串是:"+str2+'\n');

                    

                     //分離字串並存入指定陣列中

                     String str3="Java2";

                     char b[]=str3.toCharArray();

                     System.out.println("轉存入之陣列內容:");

                     System.out.println(b[0]+"、"+b[1]+"、"+b[2]+"、"+b[3]+'\n');

                    

                     //依索引值轉入

                     char c[]=newchar[6];   //先宣告字元陣列

                     str3.getChars(0,4,c,2);

                     System.out.println("陣列內容:");

                     System.out.println(c[0]+"、"+c[1]+"、"+c[2]+"、"+c[3]+"、"+c[4]+ "、"+c[5]+'\n');

                }

}

 

 

Java Script

 

指定位置[5],並取得其字元是:S

指定位置[4],並取得其字元是:

 

指定取得字串的範圍,(5,11)其子字串是:Script

 

轉存入之陣列內容:

J、a、v、a

 

陣列內容:

 

------------------

 

publicclass first_Java {

                publicstaticvoid main(String[ ] args){

                     StringBuffer sb1=new StringBuffer();//建立一個空字串緩衝區物件

                     StringBuffer sb2=new StringBuffer(30);//建立一個容量為30的字串緩衝區物件

                     String str=new String("Java Coffer");

                     StringBuffer sb3=new StringBuffer(str);//利用字串物件建立字串緩衝區物件

                     //取得長度與容量資料

                     System.out.println("sb2的長度:"+sb2.length());

                     System.out.println("sb2的容量"+sb2.capacity());

                     System.out.println("sb3字串緩衝區的內容:"+sb3);

                     System.out.println("sb3的長度:"+sb3.length());

                     System.out.println("sb3的容量"+sb3.capacity());

                    

                     sb3.setCharAt(4,'-');//設定特定字元

                     System.out.println("\n重新設定字串緩衝區內第4個字元:"+sb3);

               }   

}

 

 

sb2的長度:0

sb2的容量30

sb3字串緩衝區的內容:Java Coffer

sb3的長度:11

sb3的容量27

 

重新設定字串緩衝區內第4個字元:Java-Coffer

 

----------

 

 

publicclass first_Java {

                publicstaticvoid main(String[] args) {

                     StringBuffer sb1 = new StringBuffer("Java");// 建立一個字串緩衝區物件

                     System.out.println("原始字串=" + sb1);

                     char ch[] = { '字', '串', '緩', '衝', '區' };// 建立一字元陣列

                     // 新增

                     sb1.append(ch, 2, 3);

                     System.out.println("新增字串陣列: " + sb1);

                     // 刪除

                     sb1.delete(4, 7);

                     System.out.println("刪除字串: " + sb1);

                     // 新增

                     sb1.append("教學實務");

                     System.out.println("新增字串: " + sb1);

                     // 插入

                     sb1.insert(6, "

");

                     System.out.println("插入字串: " + sb1);

                     int num = 2;

                     // 插入

                     sb1.insert(4, num);

                     System.out.println("插入數字: " + sb1);

                     // 取代

                     sb1.replace(4, 8, "取代字串");

                     System.out.println("字串取代: " + sb1);

                     // 反轉

                     sb1.reverse();

                     System.out.println("字串反轉: " + sb1);

                }

}

 

原始字串=Java

新增字串陣列: Java緩衝區

刪除字串: Java

新增字串: Java教學實務

插入字串: Java教學與實務

插入數字: Java2教學與實務

字串取代: Java取代字串實務

字串反轉: 務實串字代取avaJ

 

----------------

 

 

publicclass first_Java {

                publicstaticvoid main(String[] args){

                     StringBuffer  sb=new StringBuffer("Programming is funny");

                               

                     System.out.println("示範字串:"+sb);

                     // 計算字串長度

                     System.out.println("字串長度="+sb.length());

                     // 計算字串容量

                     System.out.println("字串容量="+sb.capacity());

                }

}

 

 

示範字串:Programming is funny

字串長度=20

字串容量=36

 

 

-------

 

publicclass first_Java {

                publicstaticvoid main(String[] args) {

                     StringBuffer sb = new StringBuffer("Hello Java");

 

                     char a[] = newchar[12];

                     sb.getChars(6, 10, a, 0);

                     System.out.println("取得部份字串=" + sb);

                }

}

 

取得部份字串=Hello Java

 

-------------

 

publicclass first_Java {

                publicstaticvoid main(String[ ] args){

                     StringBuffer  sb=new StringBuffer("Hello Java");

                     System.out.println("示範字串:"+sb);

                     System.out.println("刪除前面[0~5]的部份字串="+sb.delete(0,5)+'\n');  

                    

                     StringBuffer  sb2=new StringBuffer("Hello Javaa");

                     System.out.println("示範字串:"+sb2);

                    

                     System.out.println("刪除指定字元="+sb2.deleteCharAt(10)+'\n');          

                }

}

 

 

示範字串:Hello Java

刪除前面[0~5]的部份字串= Java

 

示範字串:Hello Javaa

刪除指定字元=Hello Java

 

 

 

-----------

 

publicclass first_Java {

                   publicstaticvoid main(String[] args)

                   {  // 使用parseInt()方法將字串轉換成整數數值

                      int    a = Integer.parseInt("125");

                      int    b = Integer.parseInt("243");

                      int    c=a+b;

                      // 顯示數值

                      System.out.println("a+b= "+c);

                   }

}

 

a+b= 368

 

-----------

 

publicclass first_Java {

                   publicstaticvoid main(String[] args)

                   { 

                      String[] extension = {"文宣.doc", "廣告信.pdf",

                      "新聞稿.doc", "演講.ppt", "邀請函.doc"};

                      System.out.println("使用endsWith()方法來過濾屬於Word文件的檔案");

                      for(int i = 0; i < extension.length; i++)

                      if(extension[i].endsWith("doc"))

                      System.out.println(extension[i]); 

                   }

}

 

 

使用endsWith()方法來過濾屬於Word文件的檔案

文宣.doc

新聞稿.doc

邀請函.doc

 

 

------

 

publicclass first_Java {

                   publicstaticvoid main(String[] args)

                   { 

                      String str1 =

                      "cab         n.       計程車        Call a cab for me.          幫我叫一輛計程車.";

                      System.out.println("使用Tab作為欄位分割字元");

                      String[] field1 = str1.split("\t");

                      for(String field : field1) {

                            System.out.print(field + "\t");

                      }

                      System.out.println("\n=================================================");

                      String str2 =

                      "fluent,adj.,流利的,They speak fluent English.,他們的英語很流利.";

                      System.out.println("使用,逗號作為欄位分割字元");

                      String[] field2 = str2.split(",");

                      for(String field : field2) {

                            System.out.print(field + "\t");

                      }

                      System.out.println("\n=================================================");

                      String str3 =

                      "hibernate;v.;冬眼;Some bears hibernate.;有些熊會冬眠.";

                      System.out.println("使用;分號作為欄位分割字元");

                      String[] field3 = str3.split(";");

                      for(String field : field3) {

                            System.out.print(field + "\t");

                      }

                      System.out.println("\n=================================================");

                   }

}

 

 

使用Tab作為欄位分割字元

cab         n.  計程車        Call a cab for me.          幫我叫一輛計程車.      

=================================================

使用,逗號作為欄位分割字元

fluent     adj.         流利的        They speak fluent English.      他們的英語很流利.      

=================================================

使用;分號作為欄位分割字元

hibernate    v.        冬眼  Some bears hibernate.   有些熊會冬眠.    

 

 

 

---------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

作業:

 

心得部份需滿10   可上網找相關資料相關資料

 

第一組:

(1)     請設計一程式 求出字串“APPLE”的長度並將其轉換成小寫

(2)     在字串“APPLE”後面附加一字串“JAVA”  將其顯示  再刪除新字串的第二和第三個字元

 

 

第二組:

(1)    設計程式  分別將數字3456及布林值true轉換成字串

(2)    考慮大小寫   設計程式比較"JAVA" "java"

  

 

第三組:

(1)    自訂題目  需用到   append  insert  delete replace  reverse 這些功能

(2)    請設計一程式 找出字元C在字串 “ABCDEFG”出現的索引位置

 

 

 

加分題

範例中有一題輸出有問題

 

publicclass first_Java {

                publicstaticvoid main(String[] args){

                     // 宣告字串物件

                     String str1="Java Script";

                     System.out.println(str1+'\n');  //'\n'是換行

                    

                     //擷取指定位置的字元

                     char a1=str1.charAt(5);

                     char a2=str1.charAt(4);

                     System.out.println("指定位置[5],並取得其字元是:"+a1);

                     System.out.println("指定位置[4],並取得其字元是:"+a2+'\n');

                    

                     //取得子字串

                     String str2=str1.substring(5,11);

                     System.out.println("指定取得字串的範圍,(5,11)其子字串是:"+str2+'\n');

                    

                     //分離字串並存入指定陣列中

                     String str3="Java2";

                     char b[]=str3.toCharArray();

                     System.out.println("轉存入之陣列內容:");

                     System.out.println(b[0]+"、"+b[1]+"、"+b[2]+"、"+b[3]+'\n');

                    

                     //依索引值轉入

                     char c[]=newchar[6];   //先宣告字元陣列

                     str3.getChars(0,4,c,2);

                     System.out.println("陣列內容:");

                     System.out.println(c[0]+"、"+c[1]+"、"+c[2]+"、"+c[3]+"、"+c[4]+ "、"+c[5]+'\n');

                }

}

 

 

Java Script

 

指定位置[5],並取得其字元是:S

指定位置[4],並取得其字元是:

 

指定取得字串的範圍,(5,11)其子字串是:Script

 

轉存入之陣列內容:

J、a、v、a

 

陣列內容:

 

 

找出問題在哪   email告訴我可整組加分

 

 

還有登陸月球的程式碼   依介紹多寡來決定加分多少  介紹越多加分越多 (需報告  請先看熟要介紹的部份)

arrow
arrow
    全站熱搜

    pcwiki 發表在 痞客邦 留言(0) 人氣()