public class ReplaceVowels {
public static void main(String args[]) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the String:");
String str = bf.readLine();
char[] c = new char[str.length()];
for (int i = 0; i < str.length(); i++) {
if (c[i] == 'a' || c[i] == 'e' || c[i] == 'i' || c[i] == 'o'
|| c[i] == 'u') {
System.out.println(str.replace(c[i], '?'));
}
}
}
}
爲什麼str.replace
方法不起作用?我應該怎麼做才能使它工作?如何用Java中的特殊字符替換元音?
謝謝你們欣賞它 – keith 2012-11-05 20:24:12
字符串是不可改變的,'str.replace'不會修改原始的字符串,它將返回新/獨立的一個修改後的內容。你可能意思是'str = str.replace(...)'。 – Pshemo 2017-11-29 17:36:05