【发布时间】:2017-07-26 16:08:41
【问题描述】:
我想在每个元音之前添加“OB”。 示例输入:“这是一个测试” 示例输出:“THOBIS OBIS OBA TOBEST” 我不知道为什么我的代码不起作用:
public static String obify(String test) {
int x = 0;
while (x != -1) {
if (test.charAt(x) == 'A' || test.charAt(x) == 'E' || test.charAt(x) == 'I' || test.charAt(x) == 'O' || test.charAt(x) == 'U') {
test = test.replace(test.substring(x, x+1), "ob" + test.substring(x, x+1));
x += 3;
} else {
x++;
}
if (x >= test.length() - 1) {
x = -1;
}
}
return test;
}
【问题讨论】:
-
你也可以只使用
input.replaceAll("A", "OBA").replaceAll("E", "OBE")等等,或者直接使用正则表达式input.replaceAll("([AEIOU])", "OB$1")。另外,您当前解决方案的输出是什么? -
在什么情况下它不起作用?它是否因错误而失败?如果是这样,请向我们展示堆栈跟踪。输出不正确吗?如果是这样,请告诉我们你得到了什么。
-
replace方法替换其第一个参数的所有货币。
标签: java string loops character