我在最近的 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/