这个问题在这里已经有了答案:
stringbuilder.delete失败,长度有效(1个答案)
22天前关门了。
我想删除字符串的某些部分,但它不起作用。有人能帮我纠正我的代码吗,我的代码中唯一显示的是
The Substring starting from index 0: PARAPAPAMPAM
然后是
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: start 9, end 4, length 12
at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(AbstractStringBuilder.java:1802)
at java.base/java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:912)
at java.base/java.lang.StringBuffer.delete(StringBuffer.java:469)
at engage.num9.main(num9.java:10)
不管怎样,这是我的密码
public class num9 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("PARAPAPAMPAM");
System.out.println("The Substring starting from index 0:");
System.out.println(sb.substring(0));
sb.delete(9, 4);
System.out.println("The new Substring is: " + sb);
}
}
1条答案
按热度按时间eh57zj3b1#
将鼠标光标悬停在“delete”上,指向某人。delete(9,4)。eclipse将向您显示delete()方法的描述。上面写着
所以你必须重写你的代码