【发布时间】:2021-08-12 18:28:13
【问题描述】:
我不断得到 String index out of bound: 22 异常。谁能告诉我我做错了什么?代码如下:
class Troll {
private static final char[] VOWELS = {'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'};
public static String disemvowel(String str) {
int lengthStr = str.length();
int lengthVOWELS = VOWELS.length;
for (int i = 0; i < lengthStr; ++i)
for (char vowel : VOWELS)
if (str.charAt(i) == vowel)
str = removeChar(str, i);
return str;
}
private static String removeChar(String str, int index) {
return str.substring(0, index) + str.substring(index + 1);
}
}
我在这里要做的是编写一个函数,该函数接受一个字符串并返回一个删除所有元音的新字符串。
【问题讨论】:
标签: java string exception indexoutofboundsexception