gpt4 book ai didi

java - 在java中删除字符串的最后一部分

转载 作者:搜寻专家 更新时间:2023-11-01 01:00:11 25 4
gpt4 key购买 nike

String Y="part1 part2 part3",X="part1";

boolean foundMatch = false;
while(!foundMatch) {
foundMatch = Y.equals(X);
if(foundMatch) {
break;
}
else {
Y = useSplitToRemoveLastPart(Y);
if(Y.equals("")) {
break;
}
}

//useSplitToRemoveLastPart()的实现

private static String useSplitToRemoveLastPart(String y) {

//What goes here .. It should chop the last part of the string..
return null;

}

谁能帮忙...

最佳答案

如果要去掉part3,前提是所有单词之间用空格隔开

String str ="part1 part2 part3";

String result = str.substring(0,str.lastIndexOf(" "));

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

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