gpt4 book ai didi

java - 从字符串生成器中删除字符

转载 作者:行者123 更新时间:2023-11-30 02:40:35 25 4
gpt4 key购买 nike

我正在尝试运行一个循环来从字符串中删除一些字符。但是当我运行以下代码时,我仅从第一次运行中获得输出(I on)。我不明白字符串的其余部分。有人可以帮助我在这里添加什么吗?仅显示第一次迭代的结果。谢谢

someStr = "I don't know this";
StringBuilder sb = new StringBuilder(someStr);
int n = 3
for (int i = n - 1; i < sb.length(); i = n + 1) {
sb = sb.deleteCharAt(i);
}
System.out.println(sb.toString());

最佳答案

for 语句的第三部分是增加或减少索引的指令。

那里,它总是 4。

更清楚一点:

1st iteration : i = 2 => you remove the 'd', your string is now "I on't know this"

2nd iteration : i = 4 => you remove the ''', your string is now "I ont know this"

3rd iteration : i = 4 => you remove the 't', your string is now "I on know this"

4th iteration : i = 4 => you remove the ' ', your string is now "I onknow this"

...

关于java - 从字符串生成器中删除字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41857131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com