/*插入字串*/ import java.io.*; class Sample4_insert { public static void main(String[] args) throws IOException { System.out.println("請輸入字串。"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str1=br.readLine(); //讀取從鍵盤輸入的第一個字串 System.out.println("請輸入要新增的字串。"); String str2=br.readLine(); //讀取從鍵盤輸入的第二個字串 System.out.println("要插入到第一字串的第幾個字之中?"); String str3=br.readLine(); //讀取從鍵盤輸入的數值 int num=Integer.parseInt(str3); //將字串轉換成數值 StringBuffer sb=new StringBuffer(str1); sb.insert(num,str2); //第二個字串會插入在第幾個字串之中 System.out.println("在「" + str1 + "」中插入「" + str2 + "」的話,就會變成「" + sb + "」。"); } }
2011年11月7日 星期一
JAVA 範例 ch10 Sample4_insert
插入字串
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言