我很难弄清楚为什么我的代码不起作用。我试图停止特定字母的输出,但它会继续迭代整个字符串。这就是我所拥有的,
public static char stringIterator(String string) {
System.out.println(string);//Keep this line
char lastChar = string.charAt(string.length() - 1);
if (lastChar == 'M') {
return lastChar;
}
else if (string.length() == 1) {
return lastChar;
}
else {
return stringIterator(string.substring(0, string.length() - 2));
}
}
最佳答案
如果你只想看看它是否有它那么你可以使用
string.contains('char');
如果你想遍历/迭代那么
for( int i = 0; i < string.length(); i++)
if(string.at(i) == '#')
{ //whatever you want here
}
关于java - 如何遍历字符串来查找特定字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55173778/