gpt4 book ai didi

java - 从字符串中删除最后一个字符

转载 作者:行者123 更新时间:2023-12-01 20:02:05 25 4
gpt4 key购买 nike

我在最近的 Java 项目中遇到了问题。我试图从字符串中取出字符串“白色”。无论我尝试什么方法,最后一个“_”始终保留。

    String questionText = "The white house is _white_";
String correctResponse = questionText.replace(questionText.substring(0, questionText.indexOf("_")+1), "");
correctResponse.substring(0,correctResponse.length()-1);
System.out.println(correctResponse);

最佳答案

子字符串不修改原始对象。

使用

 correctResponse = correctResponse.substring(0, correctResponse.length() - 1);

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

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