你有replace()
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence");
String sentence = in.nextLine();
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == 'a' || sentence.charAt(i) == 'e' || sentence.charAt(i) == 'i'
|| sentence.charAt(i) == 'o' || sentence.charAt(i) == 'u') {
// Assign the results of replace() back to sentence
sentence = sentence.replace(sentence.charAt(i), ' ');
}
}
System.out.println(sentence);
}
您能抽出自己for
环和长篇大论if
与replaceAll()
后重新分配句子使用图案
"[aAeEiIoOuU]"
个
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence");
String sentence = in.nextLine();
// Replace all english upper-case & lower-case vowels
System.out.println(sentence.replaceAll("[aAeEiIoOuU]", " "));
}
结果:
Enter a sentence
thE Quick Brown FOX
th Q ck Br wn F X
你试过'句子= sentence.replace(sentence.charAt(我),'“);' – 2015-03-08 19:31:15
现在我有。感谢您的更正现在它工作 – ema 2015-03-08 19:33:15
关闭作为重复更好的候选人:[提示java.lang.String.replace问题?](http://stackoverflow.com/q/1166905/217324) – 2015-07-15 19:10:11