我想用Apple和Lenovo取代所有的元音。这样的东西作为输出。 %20ppl%20, L%20n%20v%20
这是我的代码
String[] dalek = new String[]{"Apple","Lenovo"};
String[] wy = new String[]{"a","e","i","o","u"};
for (int i = 0 ; i < dalek.length; i++ )
String wyx = wy[i];
dalek[i] = dalek[i].replace(wyx,"%20");
System.out.println(Arrays.toString(dalek));
答案 0 :(得分:5)
你的循环没有大括号。
这意味着只有String wyx = wy[i];
将在循环中执行,而不是其余的。
这是一个更优雅的解决方案:
String[] dalek = new String[]{"Apple","Lenovo"};
String[] replaced = new String[dalek.length];
for (int i = 0; i < dalek.length; i++) {
// assigning repalced[i]
// | with dalek at index i
// | | case insensitive
// | | | vowels class (add "y" if necessary)
// | | | | with URL-encoded space
replaced[i] = dalek[i].replaceAll("(?i)[aeiou]", "%20");
}
System.out.println(Arrays.toString(replaced));
<强>输出强>
[%20ppl%20, L%20n%20v%20]
答案 1 :(得分:0)
这应该有效:
String[] dalek = new String[]{"Apple","Lenovo"};
String[] wy = new String[]{"a","e","i","o","u"};
for (int i = 0 ; i < dalek.length; i++ ) {
for (int j=0; j<wy.length;j++){
String wyx = wy[j];
dalek[i] = dalek[i].toLowerCase().replace(wyx,"%20");
}
}
System.out.println(Arrays.toString(dalek));
答案 2 :(得分:0)
这可能会有所帮助
String[] dalek = new String[] { "Apple", "Lenovo" };
String[] wy = new String[] { "a", "e", "i", "o", "u" };
for (int i = 0; i < dalek.length; i++) {
for (int j = 0; j < wy.length; j++) {
String wyx = wy[j];
dalek[i] = dalek[i].replaceAll("(?i)[" + wyx + "]", "%20");
}
}
System.out.println(Arrays.toString(dalek));
答案 3 :(得分:0)
String[] dalek = new String[]{"Apple","Lenovo"};
String[] wy = new String[]{"a","e","i","o","u"};
for (int i = 0 ; i < dalek.length; i++ )
{
for(int j=0;j<wy.length;j++)
{
dalek[i] = dalek[i].replaceAll("(?i)"+wy[j], "%20");
}
}
System.out.println(Arrays.toString(dalek));
这有效