請撰寫程式,提供一個static方法myReplace(),模擬String類別的replace()方法。

 

 

import java.io.*;
public class app2 { 

public static void main(String[] args) throws IOException { 

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

 System.out.print("請輸入字串:"); 
 String str = br.readLine(); // 讀取


 System.out.print("請輸入要取代的字元:"); 
 String oldChar = br.readLine(); // 讀取


 System.out.print("請輸入要取代成那個字元:"); 
 String newChar = br.readLine(); // 讀取

 System.out.println(myReplace(str,oldChar.charAt(0),newChar.charAt(0))); 


static String myReplace(String str,char oldChar,char newChar) { 
   char[] s = str.toCharArray(); //將字串轉換為 char 字元陣列 
   for(int i=0;i<str.length();i++) { 
   if(s[i]==oldChar) s[i] = newChar; 

     return (new String(s)); 

 

 


 

(不好意思小宣傳一下我的繪圖 希望大家喜歡)

 

\\

這是我的海苔貓粉專

www.facebook.com/norinekocat/

 

海苔貓IG

海苔貓Instagram

 

謝謝大家:))

裡面有許多繪圖作品~ 希望大家支持我會繼續努力

請多多指教哦

 

 

ps

p新的貼圖終於出來了💜💜💜可愛的海苔貓-第三代

 

海苔貓繪圖作品\\

希望大家支持~~

arrow
arrow
    文章標籤
    解答 Java
    全站熱搜

    海苔貓Nori 發表在 痞客邦 留言(0) 人氣()