Java 从字符串中删除最后一个字符
我在最近的一个Java项目中遇到了问题。我正试图从字符串中删除字符串“白色”。无论我尝试什么方法,最后一个“u”总是存在Java 从字符串中删除最后一个字符,java,string,Java,String,我在最近的一个Java项目中遇到了问题。我正试图从字符串中删除字符串“白色”。无论我尝试什么方法,最后一个“u”总是存在 String questionText = "The white house is _white_"; String correctResponse = questionText.replace(questionText.substring(0, questionText.indexOf("_")+1), ""); correctResponse.su
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);
使用
lastIndexOf
String correctResponse = questionText.replace(questionText.substring(questionText.indexOf("_"), questionText.lastIndexOf("_")+1), "");
使用
lastIndexOf
String correctResponse = questionText.replace(questionText.substring(questionText.indexOf("_"), questionText.lastIndexOf("_")+1), "");
我将使用正则表达式在下划线之间对所有内容进行分组,然后
String.replaceAll(String,String)
实际删除组之外的所有内容。像
String correctResponse = questionText.replaceAll(".+\\s+_(.+)_", "$1"); // white
我将使用正则表达式在下划线之间对所有内容进行分组,然后
String.replaceAll(String,String)
实际删除组之外的所有内容。像
String correctResponse = questionText.replaceAll(".+\\s+_(.+)_", "$1"); // white
子字符串
不修改原始对象
使用
子字符串
不修改原始对象
使用
如果所需字符串始终位于下划线之间(或至少在一个下划线之后),则可以拆分该字符串并在索引1处获取子字符串:
String correctResponse = questionText.split("_")[1];
如果所需字符串始终位于下划线之间(或至少在一个下划线之后),则可以拆分该字符串并在索引1处获取子字符串:
String correctResponse = questionText.split("_")[1];
你觉得很复杂-为什么你需要更换?使用子字符串可以实现同样的效果 第一句话
String correctResponse = questionText.substring(questionText.indexOf("_")+1)
// ==> correctResponse = "white_"
correctResponse = correctResponse.substring(0, correctResponse.indexOf("_"))
// ==> correctResponse = "white"
第二项声明
String correctResponse = questionText.substring(questionText.indexOf("_")+1)
// ==> correctResponse = "white_"
correctResponse = correctResponse.substring(0, correctResponse.indexOf("_"))
// ==> correctResponse = "white"
正如@neuo指出的,子字符串不会改变字符串 你觉得很复杂-为什么需要更换?使用子字符串可以实现同样的效果 第一句话
String correctResponse = questionText.substring(questionText.indexOf("_")+1)
// ==> correctResponse = "white_"
correctResponse = correctResponse.substring(0, correctResponse.indexOf("_"))
// ==> correctResponse = "white"
第二项声明
String correctResponse = questionText.substring(questionText.indexOf("_")+1)
// ==> correctResponse = "white_"
correctResponse = correctResponse.substring(0, correctResponse.indexOf("_"))
// ==> correctResponse = "white"
正如@neuo指出的,子字符串不会改变字符串 您只需更改第3行 原始行: correctResponse.substring(0,correctResponse.length()-1) 正确的行:
correctResponse=correctResponse.substring(0,correctResponse.length()-1) 您只需更改第3行 原始行: correctResponse.substring(0,correctResponse.length()-1) 正确的行:
correctResponse=correctResponse.substring(0,correctResponse.length()-1) 如果使用正则表达式,则不必检查索引边界
String string = "Merry Christmas!".replaceAll(".$", "");
System.out.println(string);
将打印出来
Merry Christmas
如果使用正则表达式,则不必检查索引边界
String string = "Merry Christmas!".replaceAll(".$", "");
System.out.println(string);
将打印出来
Merry Christmas
就像使用
String correctResponse=questionText.replace(questionText.substring(0,questionText.indexOf(“”)+1)”)一样代码>就像使用String correctResponse=questionText.replace(questionText.substring(0,questionText.indexOf(“”)+1)”)一样代码>谢谢!这正是我想要的。警告:如果下划线在字符串中出现两次以上,此解决方案将失败。这个字符串将失败:白宫是白色的。正则表达式的答案可能是这里最安全的赌注。在这种情况下,这将起作用:String[]stringPieces=questionText.split(“”);String correctResponse=stringPieces[stringPieces.length-1]代码>谢谢!这正是我想要的。警告:如果下划线在字符串中出现两次以上,此解决方案将失败。这个字符串将失败:白宫是白色的。正则表达式的答案可能是这里最安全的赌注。在这种情况下,这将起作用:String[]stringPieces=questionText.split(“”);String correctResponse=stringPieces[stringPieces.length-1]代码>